关于android中的服务通知,我有两个问题:
如果我选择重启设备,如何取消服务?
如果我关闭我的设备并重新启动它,如何保持服务运行?
关于android中的服务通知,我有两个问题:
如果我选择重启设备,如何取消服务?
如果我关闭我的设备并重新启动它,如何保持服务运行?
您只需在清单文件中调用 bootcompiled 即可重新创建服务。
见下面的代码:
<!-- To receive the Alarm Notification -->
<receiver android:name=".RecreateTwoMonthAlarm" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
现在,如果您不想在设备重新启动时重新创建服务,那么只需在清单中定义接收器,无需任何 BOOT_COMPLETED 操作。
就像下面的代码:
<receiver android:name=".AlarmReceiverNotificationForTwoMonth" />
这是我的情况下的接收器。如果您想要同样的服务,那么您也可以这样做。
希望它会帮助你。
1)如果我选择重启我的设备,如何取消服务?无事可做,如果没有 BOOT_COMLETED 接收器。
2)如果我关闭我的设备并重新启动它,如何保持服务运行?您应该创建一个接收器类:
public class StartupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context,YourService.class));
}
}
并在清单文件中声明:
<receiver android:name=".StartupReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>