Search code examples
androidbroadcastreceiveralarmmanagerdatetimepickerandroid-pendingintent

AlarmManager Pending intent not working with broadcast receiver


I am working on an App in which i want to set custom alarm.but after setting a time on alarm nothing happens. i am sharing my code. Please help and guide me if something is missing in my code.

In my MainActivity.java

        @Override
          protected Dialog onCreateDialog(int id) {

                if (id == DIALOG_ID) {
        return new TimePickerDialog(MainActivity.this, kTimePickerListener, hour, min, false);
    }
    return null;
}

protected TimePickerDialog.OnTimeSetListener kTimePickerListener =
        new TimePickerDialog.OnTimeSetListener() {

            @Override
            public void onTimeSet(TimePicker timePicker, int hourofday, int minutes) {
                hour = hourofday;
                min = minutes;

                Calendar calendar = Calendar.getInstance();

                // set selected time from timepicker to calendar

                calendar.set(Calendar.HOUR_OF_DAY, hour);
                calendar.set(Calendar.MINUTE, minutes);

                Intent myIntent = new Intent(MainActivity.this, ServiceManager.class);
                myIntent.putExtra("reqcode", 11);

                // A PendingIntent specifies an action to take in the
                // future
                PendingIntent mPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 11, myIntent, 0);

                // set alarm time
                 alarmManager = (AlarmManager) MainActivity.this
                        .getSystemService(Context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), mPendingIntent);

            }
        };

In My ServiceManager.class

           public class ServiceManager extends BroadcastReceiver {



@Override
public void onReceive(Context context, Intent intent) {

    int Noti_code = intent.getIntExtra("reqcode",-1);


        Intent myIntent = new Intent(context, NotificationService.class);
        myIntent.putExtra("reqcode", Noti_code);
        context.startService(myIntent);


}
}

In My NotificationService.class

    public class NotificationService extends Service {

private NotificationManager mManager;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
}

@SuppressWarnings({"static-access", "deprecation"})
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    int Noti_Code = intent.getIntExtra("reqcode",-2);

    if (Noti_Code == 11) {
        mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
        ShowNotification(getApplicationContext(), mManager);
    } 


}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}
}

In my Manifest.xml

     <uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Activity_detail" />
    <activity android:name=".History" />

    <service
        android:name=".NotificationService"
        android:enabled="true"
        android:exported="true"
        android:stopWithTask="false"/>
    <receiver android:name=".ServiceManager"
        android:process=":remote"/>
</application>

I am checking through debugging broadcast receiver doesn't called. Please help me if i am missing any thing. Also i want to know how many pending intent can be fire at a time and will all works one by one?

Thanks.

Refrence links:

AlarmManager wont fire pending intent

http://code4reference.com/2012/07/tutorial-on-android-alarmmanager/

BroadcastReceiver and AlarmManager not working with

AlarmManager is not working after app is closed? - Android


Solution

  • You have your BroadcastReceiver running in another process. If you are setting breakpoints with a debugger you will miss this. If you don't need your BroadcastReceiver running in a separate process, remove the

    android:process=":remote"
    

    declaration from the manifest.

    See the documentation about how the android:process specifier works.