1

I have a GPS Service that gets GPS position every 60 seconds. It's working okay, but it doesn't do anything after phone reboot. I tried adding this in a BroadcastReceiver that is working after reboot but nothing happens. Any help would be great; I just need to load my Intents after reboot.

//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;
}

Manifest

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

3 回答 3

3

在您的清单中:

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

还要在清单中添加权限:

<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);
    }
于 2013-06-17T05:21:36.080 回答
1

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

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
于 2013-06-17T05:16:27.560 回答
0
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        context.startService(new Intent(context, GPSActivity.class));
    }
于 2013-06-17T05:24:05.217 回答