我有一个 GPS 服务,每 60 秒获取一次 GPS 位置。它工作正常,但在手机重启后它没有做任何事情。我尝试将其添加到重启后可以正常工作的 BroadcastReceiver
中,但没有任何反应。任何帮助都会很棒;我只需要在重启后加载我的 Intent 。
//Start intents after reboot
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
context.startService(new Intent(context, DashboardActivity.class));
}
GPSActivity.java
public int onStartCommand(Intent intent, int flags, int startId) {
//Toast.makeText(getBaseContext(), "Service Started", Toast.LENGTH_SHORT).show();
final Runnable r = new Runnable() {
public void run() {
Log.v("GPS_TRACKER", "Run Start");
location();
handler.postDelayed(this, 60000);
}
};
handler.postDelayed(r, 60000);
return START_STICKY;
}
list
<!-- GPS Activity -->
<service android:enabled="true" android:name=".GPSActivity">
<intent-filter>
<action android:name="com.ni.androidtrack.GPSActivity"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</service>
<!-- Also permission for RECEIVE_BOOT_COMPLETED -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
最佳答案
在您的 list 中:
<service android:exported="false" android:name=".service.YourService" android:enabled="true"></service>
<receiver android:name=".service.YourBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
同时在 list 中添加权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
你的启动接收器:
public class YourBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Intent serviceIntent = new Intent(arg0, YourService.class);
arg0.startService(serviceIntent);
}
关于android - 重启后启动 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17140779/