2013-09-24 64 views
0

即時嘗試啓動設備啓動時的應用程序。啓動在設備啓動時啓動活動的服務

我的代碼作爲跟隨

1-首先是包含一個後臺線程(的AsyncTask),其將數據發送到一個MySQL數據庫的主類。

2- Service類

package com.seven.ex.helper; 

import com.seven.ex.AndroidGPSTrackingActivity; 

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

public class service extends Service { 
private static final String TAG = "MyService"; 
@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 
public void onDestroy() { 
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onDestroy"); 
} 

@Override 
public void onStart(Intent intent, int startid) 
{ 
    Intent intents = new Intent(getBaseContext(),AndroidGPSTrackingActivity.class); 
    intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    startActivity(intents); 
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); 
    Log.d(TAG, "onStart"); 
} 
}  

3- BootUpReceiver類

package com.seven.ex.helper; 

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

public class BootUpReceiver extends BroadcastReceiver{ 

@Override 
public void onReceive(Context arg0, Intent arg1) { 
    Intent intent = new Intent(arg0,service.class); 
    arg0.startService(intent); 
    Log.i("Autostart", "started"); 
} 

} 

4- Android清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.gpstracking" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="8" /> 

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

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




    <application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name="com.seven.gpstracking.AndroidGPSTrackingActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 


    <service android:name=".service" 
       android:label="@string/app_name" 
       > 
    </service> 

    <receiver android:enabled="true" android:name=".BootUpReceiver"> 

    <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
    </receiver>   

    </application> 


    </manifest> 

主要方法正常工作之前,我試圖啓動它啓動。 另外,應用程序仍在工作後,我忽略啓動(應用程序關閉)

Android的錯誤日誌貓正在逐漸未發現異常

可以請你幫一個類?

+0

代碼無法找到服務或AndroidGPSTrackingActivity類嗎? – cYrixmorten

+0

當我啓動模擬器時出現強制關閉錯誤。那麼如果我通過點擊圖標打開應用程序,它就可以順利運行。 logcat不顯示任何東西,直到我通過點擊圖標打開應用程序 – Seven

+0

我明白,但你寫「在日誌貓得到一個類沒有發現異常」,我想知道它無法找到什麼類。 – cYrixmorten

回答

0

在你的清單中你已經指定了包:com.example.gpstracking。當你定義.BootUpReceiver。系統應該期望在com.example.gpstracking.BootUpReceiver中定位該類。

請嘗試將包.BootUpReceiver更改爲完整路徑com.seven.ex.helper.BootUpReceiver。據我所知,這應該是com.seven.ex.AndroidGPSTrackingActivity同樣適用於AndroidGPSTrackingActivity。

+0

複製粘貼是ab **** – bofredo

+0

@bofredo idd可以:)尤其是在XML等 - 沒有編譯器或皮棉來拯救你。在這種情況下,無論如何... – cYrixmorten

+0

它的工作原理...謝謝..我下載了一個簡單的源代碼,它讀取GPS數據並開始工作,然後我重構(重命名)包名稱,我認爲日食會改變即使在清單文件中,它的名字也是無處不在,但它沒有。非常感謝你 ;我會接受你的答案@cYrixmorten – Seven

1

您的服務不會像現在這樣工作。你將不得不從onStart()移動到onStartCommand(),然後返回該服務是否粘滯。問題在於onStart()方法很有可能根本沒有被調用(因爲它現在被deprectaed了)。

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

     //do your stuff here 

    return Service.START_NOT_STICKY; 
}