0

有很多與此主題相關的問題。但我不能讓它在模擬器5.0我的設備4.2.1運行工作正常:Android廣播接收器未觸發

這裏的清單:

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

<uses-sdk 
    android:minSdkVersion="10" 
    android:targetSdkVersion="21" /> 

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

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <receiver 
     android:name=".MyBroadcastReceiver" 
     android:enabled="true" 
     android:label="MyBroadcastReceiver" > 
     <intent-filter android:priority="1001" > 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 

    <activity 
     android:name=".MainActivity" 
     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=".PingService" > 
    </service> 
    <service android:name=".UpdateService" > 
    </service> 

    <receiver android:name=".OnBootReceiver" > 
     <intent-filter android:priority="1001" > 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
</application> 
</manifest> 

我嘗試了與2個接收器:他們沒有工作。應該寫日誌文件到SD,它從啓動活動做工精細接收機的 這裏之一:

public class MyBroadcastReceiver extends BroadcastReceiver { 
// Restart service every 30 seconds 
private static final long REPEAT_TIME = 1000 * 10; 

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.e("MyBroadcastReceiver", "BroadcastReceiver"); 
    appendLog("MyBroadcastReceiver BroadcastReceiver \n"); 
    AlarmManager service = (AlarmManager) context 
      .getSystemService(Context.ALARM_SERVICE); 
    Intent i = new Intent(context, UpdateService.class); 
    PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, 
      PendingIntent.FLAG_CANCEL_CURRENT); 
    Calendar cal = Calendar.getInstance(); 

    cal.add(Calendar.SECOND, 30); 

    service.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
      cal.getTimeInMillis(), REPEAT_TIME, pending); 

    Log.wtf("MyBroadcastReceiver", "Alarm went off"); 



} 

public static void appendLog(String text) { 
    File logFile = new File("sdcard/logfile.txt"); 
    if (!logFile.exists()) { 
     try { 
      logFile.createNewFile(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    try { 
     // BufferedWriter for performance, true to set append to file flag 
     BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, 
       true)); 
     buf.append(text); 
     buf.newLine(); 
     buf.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
} 

我看不到的錯誤。任何人? PS的應用程序啓動一次,所以這個:http://commonsware.com/blog/2011/07/05/boot-completed-regression.html被覆蓋。

感謝,

+0

BOOT_COMPLETED這意味着,當您的設備重新啓動並準備使用。我希望你明白它的意思。 – 2014-12-05 18:08:17

+0

@MurtazaHussain當然。上面的代碼應該讓移動完成啓動後sd卡上的日誌文件。 – user1616685 2014-12-05 19:16:12

+0

但它不工作。 – user1616685 2014-12-05 19:34:28

回答

0

我花了很長的關鍵問題是在manifest.xml

這裏的變化來得到它運行(只接收塊):

<receiver 
     android:name="com.example.bootService.MyBroadcastReceiver" 
     android:exported="true" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.PACKAGE_REPLACED" /> 

      <data 
       android:path="com.example.bootService" 
       android:scheme="package" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 

    </receiver> 

重要的是

<category android:name="android.intent.category.DEFAULT" /> 

標籤