无法重新启动广播接收器?

发布于 2024-12-28 04:25:17 字数 1107 浏览 0 评论 0原文

我正在创建一个使用服务组件的服务,我想始终在后台运行该应用程序。假设我关闭手机,当我使用手机时,我们的应用程序会自动关闭。

我正在尝试此代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以歌曲疗慰 2025-01-04 04:25:17

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.

听风念你 2025-01-04 04:25:17

您必须添加一个清单权限条目:(

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

当然您应该列出您的应用程序使用的所有其他权限)。

然后,实现BroadcastReceiver类,它应该是简单且快速的可执行文件。最好的方法是在此接收器中设置一个警报来唤醒您的服务(如果不需要像 Prahast 所写的那样让它一直运行)。

public class BootUpReceiver extends BroadcastReceiver {
@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, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
}}

然后,将 Receiver 类添加到清单文件中:

<receiver android:enabled="true" android:name=".receivers.BootUpReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

You have to add a manifest permission entry:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

(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).

public class BootUpReceiver extends BroadcastReceiver {
@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, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval, interval, pi);
}}

Then, add a Receiver class to your manifest file:

<receiver android:enabled="true" android:name=".receivers.BootUpReceiver"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文