重启后启动意图



我有一个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;
}
清单

<!-- 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" />

在你的manifest中:

<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> 

还可以在manifest中添加权限:

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

YourBootReceiver:

public class YourBootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        Intent serviceIntent = new Intent(arg0, YourService.class);
        arg0.startService(serviceIntent);
    }

您必须在清单文件中添加RECEIVE_BOOT_COMPLETED权限才能获得通知:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        context.startService(new Intent(context, GPSActivity.class));
    }

最新更新