AlarmManager 未决意图不适用于广播接收器

AlarmManager Pending intent not working with broadcast receiver

我正在开发一个应用程序,我想在其中设置自定义 alarm.but 在设置闹钟时间后没有任何反应。我正在分享我的代码。如果我的代码中缺少某些内容,请帮助和指导我。

在我的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);

            }
        };

在我的 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);


}
}

在我的 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();
}
}

在我的 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>

我正在通过调试广播接收器检查是否未调用。如果我遗漏任何东西,请帮助我。 我还想知道一次可以触发多少个待处理的意图,并且所有的都可以一个一个地工作吗?

谢谢。

参考链接:

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

您的 BroadcastReceiver 运行 在另一个进程中。如果您使用调试器设置断点,您会错过这一点。如果您不需要在单独的进程中使用 BroadcastReceiver 运行,请删除

android:process=":remote"

来自清单的声明。

请参阅 documentation 了解 android:process 说明符的工作原理。