我有一个使用后台服务的应用程序。如果我的手机关机,我的服务就关闭了。我正在尝试在手机启动时重新启动服务,但它没有启动。有谁能够帮助我?我正在使用这段代码:
<receiver android:name=".ConnectionReceiver"
android:enabled="true" android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
更新 onReceive() 函数
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("com.android.ConnectionReceiver");
context.startService(i);
}
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(null != info)
{
String state = getNetworkStateString(info.getState());
if(state.equals("Connected")){
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
loginForm.this.runOnUiThread(new Runnable() {
@Override
public void run() {
insertAllGps();
});
}
};
mTimer.scheduleAtFixedRate(mTimerTask,180000,180000);