gpt4 book ai didi

android - AlarmManager 未决 Intent 不适用于广播接收器

转载 作者:行者123 更新时间:2023-11-29 01:03:10 28 4
gpt4 key购买 nike

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

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

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

谢谢。

引用链接:

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"

来自 list 的声明。

参见 documentation关于 android:process 说明符如何工作。

关于android - AlarmManager 未决 Intent 不适用于广播接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49505524/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com