2012-05-21 65 views
0

可能重複:
Trying to start a service on boot on Android如何在設備啓動時啓動服務?

廣播接收器

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

public class StartActivityAtBoot extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
      Intent i = new Intent(context, CompareIMSI.class); 
      context.startService(i); 
     } 
    } 
} 

CompareSIM.java

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.IBinder; 
import android.telephony.TelephonyManager; 
import android.widget.Toast; 

public class CompareIMSI extends Service{ 

    Context context; 
    TelephonyManager operator; 

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

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show(); 
     //compareSIM(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     super.onStart(intent, startId); 
     Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
     compareSIM(); 
    } 

    public void compareSIM(){ 

     final String STORAGE = "Storage"; 
     SharedPreferences unique = getSharedPreferences(STORAGE, 0); 
     final String storedIMSI = unique.getString("simIMSI", ""); 
     final String currentIMSI = getSubscriberId().toString(); 

     if (!storedIMSI.equals(currentIMSI)){ 
      Intent i = new Intent(CompareIMSI.this, ScreenLockActivity.class); 
      startActivity(i); 
     } 
    } 

    public String getSubscriberId(){ 

     String IMSI = null; 
     String serviceName = Context.TELEPHONY_SERVICE; 
     TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName); 
     IMSI = m_telephonyManager.getSubscriberId(); 
     return IMSI; 
    } 
} 

我想日e應用程序啓動時啓動compareSIM服務,在啓動過程中,此服務將運行,因爲當前連接的SIM卡IMSI將被檢索並與已保存的IMSI匹配,一旦它們不同,用戶將被帶入登錄佈局。我想引導期間執行此,但未能如願......請諮詢我的編碼,感謝

+0

當您運行此代碼時發生了什麼? –

+0

你的服務正在啓動或不啓動? –

回答

0

您需要在Android清單註冊的廣播接收器,像這樣:

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

而且請確保您在清單中擁有此權限:

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

我已經在清單 –

+0

中做過它並且您的BroadcastReceiver是否可以在BOOT設備上運行?在那裏添加一些調試日誌並檢查logcat以查看它是否正在運行。請檢查您的服務是否正在開始。它有多遠? logcat中的任何錯誤? –

0

檢查您的androidManifest文件。您需要在androidManifest文件中添加接收器。

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

floow這些步驟在啓動,說明您的服務:

步驟1:在AndroidManifest.xml中添加BOOT_COMPLETED權限爲:

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

步驟2:在AndroidManifest。 xml將您的Reciver註冊爲:

<receiver android:name=".StartActivityAtBoot" android:label="@string/app_name"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</receiver> 

步驟3:在AndroidManifest.xml中註冊您的服務爲:

<service android:name=".CompareIMSI"> </service> 

步驟3:在StartActivityAtBoot啓動的服務爲:

public class StartActivityAtBoot extends BroadcastReceiver 
{ 
    static final String ACTION = "android.intent.action.BOOT_COMPLETED"; 
    public void onReceive(Context context, Intent intent) 
    { 
     if (intent.getAction().equals(ACTION)) 
     { 
        context.startService(new Intent(context, 
        CompareIMSI.class), null); 
      Toast.makeText(context, "CompareIMSI service has started!", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

這是所有關於啓動服務Boot.Thanks

相關問題