AlarmManager待定意图不与广播接收器一起使用

时间:2018-03-27 06:13:14

标签: android broadcastreceiver alarmmanager datetimepicker android-pendingintent

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

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


}
}

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

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

1 个答案:

答案 0 :(得分:1)

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

android:process=":remote"

清单中的声明。

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