2012-08-25 72 views
0

我試圖啓動該服務在啓動,但服務越來越被迫關閉服務強制關閉在開機

的廣播接收器的代碼如下

package com.android.locationservice; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class MyLocationBroadcastReceiver extends BroadcastReceiver{ 
    public void onReceive(Context context, Intent intent) { 
     Intent startServiceIntent = new Intent(); 
     startServiceIntent.setAction("com.android.locationservice.LocatonTraceService"); 
     context.startActivity(startServiceIntent); 
    } 
} 

和清單看這樣

<receiver android:name="com.android.locationservice.MyLocationBroadcastReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 


<service android:name=".LocationTraceService"> 
    <intent-filter> 
     <action android:name="com.android.locationservice.LocationTraceService" /> 
    </intent-filter>  
</service> 

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

和服務中實現這樣

package com.android.locationservice; 

    import android.app.Service; 
    import android.content.Intent; 
    import android.os.IBinder; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class LocationTraceService extends Service 
    { 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Toast.makeText(this, "Service created...", Toast.LENGTH_LONG).show();  
     // Log.i(tag, "Service created..."); 
     // new LocationActivity("service created..."); 

    } 




@Override 
    public void onStart(Intent intent, int startId) {  
     super.onStart(intent, startId); 

     Toast.makeText(this, "Service started...", Toast.LENGTH_LONG).show(); 
     //new LocationActivity("service started..."); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
    { 


     return super.onStartCommand(intent, flags, startId); 
    } 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show(); 
     //new LocationActivity("service destroyed..."); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

} 

的logcat的是

08-25 07:33:39.021: E/AndroidRuntime(248): FATAL EXCEPTION: main 
08-25 07:33:39.021: E/AndroidRuntime(248): java.lang.RuntimeException: Unable to start              context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? 
08-25 07:33:39.021: E/AndroidRuntime(248):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821) 
08-25 07:33:39.021: E/AndroidRuntime(248):  at android.app.ActivityThread.access$3200(ActivityThread.java:125) 
08-25 07:33:39.021: E/AndroidRuntime(248):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083) 

請幫助我,我事先是新的Android,謝謝。

+0

發佈崩潰日誌 –

回答

0

我認爲你缺少GPS權限在AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 
2

你爲什麼要啓動服務活動,而不是服務? 正確:

public class MyLocationBroadcastReceiver extends BroadcastReceiver{ 
    public void onReceive(Context context, Intent intent) { 
     startService(new Intent(context, LocatonTraceService.class)); 
    } 
}