当应用程序关闭时 BroadcastReceiver 不启动 activity
BroadcastReceiver doesn't start activity when app closed
所以,今天我已经尝试了至少五个小时来实现这一目标,而且我几乎尝试了任何可以找到的解决方案。
我正在编写一个小时钟应用程序,使用 AlarmManager 使该应用程序响铃。当我的应用程序打开或最小化时,它工作正常。我试图在应用程序关闭时让它工作,这就是问题所在。所以,有一段设置 AlarmManager 的代码:
AlarmManager am = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ctxt, AlarmService.class);
PendingIntent pen = PendingIntent.getBroadcast(ctxt, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setExact(AlarmManager.RTC_WAKEUP, nextRing.getTime(), pen);
(这里,ctxt 是上下文,我已经尝试了 getApplicationContext() 和 getBaseContext() 并且 nextRing.getTime() 是代表日期的 long)
然后,我有我的 AlarmService class(它曾经是一个服务,它解释了名称,但现在是一个 BroadcastReceiver,我只是不想现在重命名它)
public class AlarmService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/*Intent cust = new Intent(context, CustomIntent.class);
context.startService(cust);*/
Bundle extras = intent.getExtras();
Intent newIntent = new Intent(context, AlarmActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.putExtra("AlarmName", extras.getString("AlarmName"));
context.startActivity(newIntent);
}
}
所以这是仅使用 BroadcastReceiver 的尝试,显然不起作用,所以我尝试添加一个 IntentService(顶部注释掉的代码),它具有以下代码
public class CustomIntent extends IntentService {
public CustomIntent() {
super("CustomIntent");
}
@Override
protected void onHandleIntent(Intent intent) {
Intent newIntent = new Intent(getBaseContext(), AlarmActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(newIntent);
}
}
...这也不管用!最后,这是我使用的清单:
<application>
[Here goes stuff that have nothing to do with my bug]
<receiver android:name=".custom_classes.AlarmService"/>
<service
android:name="com.group9.abclock.custom_classes.CustomIntent"
android:enabled="true" >
<intent-filter>
<action android:name="com.group9.abclock.custom_classes.CustomIntent" />
</intent-filter>
</service>
</application>
抱歉(很)长 post 但我很难解释我尝试过的任何事情。如果您能提供帮助,请提前致谢!
如果您的应用程序已关闭,您将无法触发 BroadcastReceiver,因为它未注册,并且您无法使用 Context 方法,因为没有 Context。
如果您想在应用程序关闭时执行任务,您必须创建另一个带有服务的项目,然后用您的应用程序启动它。
服务在后台运行,直到有人杀死它,一旦启动,就不再需要主应用程序了。因此必须在此服务中实现响铃功能。
请记住 AlarmManager.setExact 与 API 23 并不完全一致,因为 Doze Mode。
所以,今天我已经尝试了至少五个小时来实现这一目标,而且我几乎尝试了任何可以找到的解决方案。
我正在编写一个小时钟应用程序,使用 AlarmManager 使该应用程序响铃。当我的应用程序打开或最小化时,它工作正常。我试图在应用程序关闭时让它工作,这就是问题所在。所以,有一段设置 AlarmManager 的代码:
AlarmManager am = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ctxt, AlarmService.class);
PendingIntent pen = PendingIntent.getBroadcast(ctxt, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setExact(AlarmManager.RTC_WAKEUP, nextRing.getTime(), pen);
(这里,ctxt 是上下文,我已经尝试了 getApplicationContext() 和 getBaseContext() 并且 nextRing.getTime() 是代表日期的 long)
然后,我有我的 AlarmService class(它曾经是一个服务,它解释了名称,但现在是一个 BroadcastReceiver,我只是不想现在重命名它)
public class AlarmService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/*Intent cust = new Intent(context, CustomIntent.class);
context.startService(cust);*/
Bundle extras = intent.getExtras();
Intent newIntent = new Intent(context, AlarmActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.putExtra("AlarmName", extras.getString("AlarmName"));
context.startActivity(newIntent);
}
}
所以这是仅使用 BroadcastReceiver 的尝试,显然不起作用,所以我尝试添加一个 IntentService(顶部注释掉的代码),它具有以下代码
public class CustomIntent extends IntentService {
public CustomIntent() {
super("CustomIntent");
}
@Override
protected void onHandleIntent(Intent intent) {
Intent newIntent = new Intent(getBaseContext(), AlarmActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(newIntent);
}
}
...这也不管用!最后,这是我使用的清单:
<application>
[Here goes stuff that have nothing to do with my bug]
<receiver android:name=".custom_classes.AlarmService"/>
<service
android:name="com.group9.abclock.custom_classes.CustomIntent"
android:enabled="true" >
<intent-filter>
<action android:name="com.group9.abclock.custom_classes.CustomIntent" />
</intent-filter>
</service>
</application>
抱歉(很)长 post 但我很难解释我尝试过的任何事情。如果您能提供帮助,请提前致谢!
如果您的应用程序已关闭,您将无法触发 BroadcastReceiver,因为它未注册,并且您无法使用 Context 方法,因为没有 Context。
如果您想在应用程序关闭时执行任务,您必须创建另一个带有服务的项目,然后用您的应用程序启动它。
服务在后台运行,直到有人杀死它,一旦启动,就不再需要主应用程序了。因此必须在此服务中实现响铃功能。
请记住 AlarmManager.setExact 与 API 23 并不完全一致,因为 Doze Mode。