2012-07-24 120 views
0

我的英文很差。我無法在啓動時啓動安卓服務,但我不知道問題所在。我正在嘗試示例代碼,但沒有成功。有人可以給我一個運行Java的項目嗎?其他代碼適用於其他人,但在我的平板電腦智能手機模擬器上不起作用。 Android 4.0中是否存在問題?開始Android服務4.0開機時間

這是我的代碼:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.service22" 
    android:versionCode="1" 
    android:versionName="1.0" 
    android:installLocation="internalOnly" 
    > 

    <supports-screens android:largeScreens="false" android:normalScreens="true" android:smallScreens="false"/> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name"> 
      <service android:name="com.example.MyService"> 
       <intent-filter> 
        <action android:name="com.example.MyService"> 
       </action> 
      </intent-filter> 
     </service> 
     <receiver android:name="com.example.MyReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"> 
       </action> 
       <category android:name="android.intent.category.HOME"> 
       </category> 
      </intent-filter> 
     </receiver> 
    </application> 



</manifest> 



    public class MyService extends Service 
    { 

     private static final  String LOG_TAG = "::Monitor"; 


    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.e(LOG_TAG, "Service created."); 
    } 

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

     for (int i = 0 ; i < 20 ; i++) 
     { 
      mensaje(); 
     } 



     Log.e(LOG_TAG, "Service started."); 
    } 
    @Override 
    public void onDestroy() 
    { 
      super.onDestroy(); 
      Log.e(LOG_TAG, "Service destroyed."); 
    } 

    @Override 
    public IBinder onBind(Intent intent) 
    { 
     Log.e(LOG_TAG, "Service bind."); 
     return null; 
    } 



    public void mensaje() 
    { 
     Toast.makeText(this, "Hola", Toast.LENGTH_LONG).show(); 
    } 

} 



    public class MyReceiver extends BroadcastReceiver 
{ 
    public MyReceiver() 
    { 
    } 

    String LOG_TAG = "::StartAtBootServiceReceiver"; 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 

     Log.e(LOG_TAG, "onReceive:"); 
     if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
      Intent i = new Intent(); 
      i.setAction("com.example.MyService"); 
      context.startService(i); 
     } 



    } 

} 
+0

[在啓動時Android 4.0上啓動Android服務]的可能重複(http://stackoverflow.com/questions/11640142/start-android-service-on -boot-time-android-4-0) – CommonsWare 2012-07-24 23:44:01

+0

[嘗試在Android上啓動服務時啓動服務](http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on -boot-on-android) – user3439968 2014-09-24 21:17:21

回答

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

public class OnBootReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    Log.d("OnBootReceiver", "Hi, Mom!"); 
    } 
} 

和清單文件

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <application android:icon="@drawable/cw" 
       android:label="@string/app_name"> 
    <receiver android:name=".OnBootReceiver"> 
     <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
    </application> 
+0

這是不可能的。它不工作,我已經做你的解決方案,但沒有工作。有人可以發送給我一個android proyect白衣這個?電子郵件:[email protected],我很沮喪。謝謝。 – TSW1985 2012-07-25 10:06:44

+1

@ user1549952:「它不工作」 - 是的,它的確如此。但是,在Android 3.1+上,必須在任何'BroadcastReceiver'工作之前手動運行其中一個組件。這就是爲什麼[採用這些代碼片段的完整示例項目](https://github.com/commonsguy/cw-omnibus/tree/master/SystemEvents/OnBoot)有一個供用戶啓動的活動。然後,您的服務將控制所有後續重新啓動,或者直到用戶在設置應用程序中按下強制停止(在這種情況下,您的應用程序需要在接收器工作之前再次啓動手動組件)。 – CommonsWare 2012-07-26 13:14:48

0

我想:

Intent i = new Intent(); 
i.setAction("com.example.MyService"); 
context.startService(i); 

是不夠的,你必須設置的意圖類名狀即:

Intent i = new Intent(); 
i.setClassName("com.example", "com.example.MyService"); 
i.setAction("com.example.MyService"); 
context.startService(i); 

請試試看。

0

重要提示:在以下情況下,Android OS 3.1+仍會忽略您的廣播接收器: 1.用戶從未明確啓動過應用程序至少一次。
2.用戶已經「強制關閉」應用程序。

問題與您的實施無關。這是谷歌已關閉的Android中的一個潛在安全漏洞:)