2012-02-24 67 views
1

我目前正在試圖製作一個廣播接收器,它會在android設備啓動後調用,然後運行後臺服務。我嘗試過很多例子,但不知道我要去哪裏錯。我正在關注這個例子: https://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBoot 我已經將這個整個項目導入到我的工作區中並試圖運行。但接收方沒有調用。 請幫我一把。 我測試裝置:Motorolla的Xoom與ICS 4.0.3Android引導廣播沒有調用

編輯

清單

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

<supports-screens 
    android:anyDensity="true" 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" /> 

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

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <service 
     android:name="awais.soft.MyService" 
     android:enabled="true" > 
     <intent-filter> 
      <action android:name="awais.soft.MyService" > 
      </action> 
     </intent-filter> 
    </service> 

    <receiver android:name="awais.soft.ServicesDemoActivity" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" > 
      </action> 

      <category android:name="android.intent.category.HOME" > 
      </category> 
     </intent-filter> 
    </receiver> 
</application> 

廣播接收機

package awais.soft; 

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

public class ServicesDemoActivity extends BroadcastReceiver { 

static final int idBut = Menu.FIRST + 1, idIntentID = Menu.FIRST + 2; 

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

    Log.e("Awais", "onReceive:"); 
    if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) { 
     Intent i = new Intent(); 
     i.setAction("awais.kpsoft.MyService"); 
     context.startService(i); 

    } 

} 

}

服務

​​

}

+0

沒有被寫入清單 – 2012-02-24 07:02:29

+0

這是後來的錯誤類型錯誤。 – 2012-02-24 07:04:27

+0

請發表您的編輯代碼,以便我可以得到 – 2012-02-24 07:05:39

回答

1

一切都很好..:S 的問題是與設備。(即Motorolla變焦ICS 4.0.3)

現在測試的Galaxy Tab的2.2和做工精細..

感謝所有爲您的時間

+2

問題實際上是ICS。 – 2012-06-11 10:52:40

+0

大聲笑似乎...:[ – 2012-06-11 11:21:27

+0

您需要讓用戶手動啓動您的應用程序,然後在清單中註冊的廣播接收器應該在此之後工作。除非你的應用程序在重啓之前強制關閉。 – 2012-06-11 14:41:57

1

我在我的應用程序及工作對我來說是這樣的。

public class DeviceBootReceiver extends BroadcastReceiver { 

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

     if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { 
     // CustomLog.i("Boot Completed"); 
     } 
    } 
} 

的Android Manifset

<receiver android:name=".model.service.DeviceBootReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED"></action> 
       <category android:name="android.intent.category.HOME"></category> 
      </intent-filter> 
</receiver> 

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

請檢查是否已准許項值

+0

已完成上述變化.. 。但:( – 2012-02-24 06:18:13

+0

一切都很好..:S 的問題是與設備。(即Motorolla變焦ICS 4.0.3) 現在對Galaxy Tab的測試了2.2和工作很好.. 感謝所有爲您的時間 – 2012-02-24 07:00:33

1

看到我張貼你eample,這將有助於你

對於某些應用程序,您需要在設備啓動時啓動並運行服務,無需用戶干預。這種應用程序主要包括監視器(電話,藍牙,消息,其他事件)。 至少目前由於誇大限制的Android權限政策而允許使用此功能。

第1步:首先,您需要創建一個簡單的服務,在Monitor.java定義:

public class Monitor 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); 
    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; 
} 

}

第2步:接下來我們需要創建一個廣播接收器類,StartAtBootServiceReceiver.java:

public class StartAtBootServiceReceiver extends BroadcastReceiver 
    { 
    private static final 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("test.package.Monitor"); 
     context.startService(i); 
    } 
} 

}

第3步:最後,您的AndroidManifest。XML文件必須包含以下內容:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="test.package.Monitor" 
    android:versionName="1.0" 
    android:versionCode="100" 
    android:installLocation="internalOnly"> 
    <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true" /> 

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

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

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <service android:name="test.package.Monitor">** 
     <intent-filter> 
      <action android:name="test.package.Monitor"> 
      </action> 
     </intent-filter> 
    </service> 
    <receiver android:name="test.package.StartAtBootServiceReceiver"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED"> 
      </action> 
      <category android:name="android.intent.category.HOME"> 
      </category> 
     </intent-filter> 
    </receiver> 
</application> 

我需要強調一些最重要的方面,可能存在的錯誤的關鍵因素實施:
1)許可android.permission.RECEIVE_BOOT_COMPLETED絕(在清單xml中)
2)安裝必須在內部存儲器中執行,而不是在SDCARD上執行!執行本採用Android:的installLocation = 「internalOnly」 清單中的

+0

謝謝.leme嘗試。:) – 2012-02-24 06:22:02

+0

是它的工作正常,因爲我在我的應用程序 – 2012-02-24 06:23:06

+0

ops..still使用這個不需要.. 甚至其他廣播的也不工作.. 就像我在電源連接上測試過一樣。 我想知道如何在我的清單中引入活動標籤? – 2012-02-24 06:36:20

0

如果您的手機是植根那麼你將不得不在Android的引導例廣播麻煩調用,否則你必須確保你的應用程序需要有root權限

+0

如何獲得root權限? – 2012-07-02 10:37:40

0

這個問題依然存在於Android版本超過3.0的設備中,順便說一下,它不是爲了安全目的而通過谷歌完成的問題,我猜...如果你必須在啓動時運行服務,一個自定義意圖&廣播它。爲了制定自定義的意圖,你必須製作一個服務文件,你必須在啓動完成時廣播該意圖完成&你的服務文件(你想運行)將在其onReceive方法&上收到你的服務將運行的意圖。將要創建的用於調用要運行的服務的服務文件應保存在設備的文件資源管理器的系統/應用程序文件夾中,如果您的文件系統顯示抱歉只讀文件系統,則從命令提示符執行adb remount &然後按設備上的文件,重新啓動您的系統您的服務將運行..乾杯!