0

Basically i have created alarm app but the broadcastReceiver not getting called in the marshmallow i know about the runtime permission required in the marshmallow but i don't know what will be the permission for my problem for calling broadcastReceiver.

Can anybody please suggest me something please.

Waqar Vicky
  • 373
  • 1
  • 3
  • 12
  • For alarmManager you don´t need a permission (only if you use AlarmClock and want to call an intent for set an alarm). I guess you haven´t registered the receiver inside your maniifest – Opiatefuchs Apr 28 '17 at 18:04
  • i have registered receiver in the manifest file and broadcastReceiver not fires up in all versions lower than 6.0 marshmallow but in the marshmallow broadcastreceiver not fires up when the app is not running in the background – Waqar Vicky Apr 28 '17 at 18:07
  • that sounds like doze mode is guilty for your problem. See here to implement AlarmManager the right way: https://developer.android.com/training/monitoring-device-state/doze-standby.html – Opiatefuchs Apr 28 '17 at 18:09
  • also, on some devices like Huawei or Xiaomi, you need to whitelist your app in several options...energy saving stuff.. – Opiatefuchs Apr 28 '17 at 18:10
  • yeah i facing this problem in the Huawei device. – Waqar Vicky Apr 28 '17 at 18:17
  • Yep, sucking Huawei.....go to the telephony manager-->accu manager-->protected apps--> enable your app. Then back to telephony manager menu and go to automatic app start-->enable your app. The menu names can be different, I have a german device... – Opiatefuchs Apr 28 '17 at 18:20
  • @Opiatefuchs i use this method to whitelist my app but nothing happens http://stackoverflow.com/questions/32627342/how-to-whitelist-app-in-doze-mode-android-6-0/42651399#42651399 – Waqar Vicky Apr 30 '17 at 09:13
  • and i add this line in the manifest to exclude the activities from recent apps android:excludeFromRecents="true" after adding this line my app will not remove from background and broadcastReceiver is now fires up on time is this a good practice or not? – Waqar Vicky Apr 30 '17 at 09:16
  • well, that´s interesting. This attribute should exclude your app from the list of recently used appications in the device. I don´t know how this takes effect on your problem, but if it works then do it. – Opiatefuchs May 01 '17 at 04:58
  • hehehe.... but i'm facing another problem now that my broadcast receiver not fires up when the device screen is off. – Waqar Vicky May 01 '17 at 05:00
  • do you have done the steps I mentioned above inside the device? – Opiatefuchs May 01 '17 at 05:02
  • yes i have done your steps you mention above. – Waqar Vicky May 01 '17 at 05:04
  • @Opiatefuchs thanks for your time now i slove my problem by following this link http://stackoverflow.com/questions/9477922/android-broadcast-receiver-for-screen-on-and-screen-off/9478013#9478013 now my broadcastReceiver fires up even when the screen is off. – Waqar Vicky May 01 '17 at 05:09

1 Answers1

1

I simply solve this problem by creating a service by following this link

Link

Service Code Here...

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;

/**
 * Created by waqar on 1/05/2017.
 */

public class LocalService extends Service {

    AlarmReceiver alarmReceiver;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        alarmReceiver = new AlarmReceiver();

        IntentFilter screenStateFilter = new IntentFilter();
        screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
        screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
        this.registerReceiver(alarmReceiver, screenStateFilter);

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(alarmReceiver);
    }
}

And start this service from MainActivity in onCreate Method.

Intent intent = new Intent(getApplicationContext(), LocalService.class);
getApplicationContext().startService(intent);

And don't forget to register this service in the manifest.

Community
  • 1
  • 1
Waqar Vicky
  • 373
  • 1
  • 3
  • 12