无法重新启动广播接收器?
我正在创建一个使用服务组件的服务,我想始终在后台运行该应用程序。假设我关闭手机,当我使用手机时,我们的应用程序会自动关闭。
我正在尝试此代码
androidManifest.xml
<receiver android:name=".receiver.ConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
并在 BroadCastreceiver 类中添加此代码 更新
private class ConnectionReceiver extends BroadcastReceiver{
private Timer mTimer;
private TimerTask mTimerTask;
private long interval;
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, ConnectionReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
{
calGps();
}
}
I am creating an service which is use Service Component ,I want to run the application always in background . Suppose i switch off the mobile and when i on the mobile our application is closed i.e. automatically.
am trying this code
androidManifest.xml
<receiver android:name=".receiver.ConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
and adding this code in BroadCastreceiver Class
update
private class ConnectionReceiver extends BroadcastReceiver{
private Timer mTimer;
private TimerTask mTimerTask;
private long interval;
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, ConnectionReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
{
calGps();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Leema Rose 您好,
您想在设备启动后启动您的服务/活动。检查下面的链接您将了解如何使用BroadcastReceiver。
启动时自动启动应用程序
启动时启动服务
希望对您有所帮助。
新答案:
在接收者清单文件的intent-filter中添加
。Hi Leema Rose
You want to launch your service/activity after the device booted. Check the below links you will understand how to use BroadcastReceiver.
Autostart an application at bootup
Start Service at boot
I hope it may help you.
New Answer:
Add
<category android:name="android.intent.category.HOME" />
in intent-filter of receiver's manifest file.您必须添加一个清单权限条目:(
当然您应该列出您的应用程序使用的所有其他权限)。
然后,实现BroadcastReceiver类,它应该是简单且快速的可执行文件。最好的方法是在此接收器中设置一个警报来唤醒您的服务(如果不需要像 Prahast 所写的那样让它一直运行)。
然后,将 Receiver 类添加到清单文件中:
You have to add a manifest permission entry:
(of course you should list all other permissions that your app uses).
Then, implement BroadcastReceiver class, it should be simple and fast executable. The best approach is to set an alarm in this receiver to wake up your service (if it's not necessary to keep it running ale the time as Prahast wrote).
Then, add a Receiver class to your manifest file: