2016-06-15 21 views
0

我有一個名爲Bootup的擴展類的廣播接收器,我希望在設備關閉後第一次啓動時調用它。我已將其實施到其他應用程序中,因此我將該代碼遷移到此應用程序並更改了所有必要的詳細信息。沒有Logcat打印或來自啓動接收器的通知

這是它是如何應用標籤下注冊的清單XML文件中:

<receiver android:enabled="true" android:name=".reveivers.Bootup" 
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</receiver> 

這是類本身:

public class Bootup extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     //Received when the phone boots up 

     //Do some stuff 

     //Test notification to check if it worked. Doesn't show, but the method is valid code. 
     playNotification(context, "Active English", "Bootup received!"); 

     //More code, then this bit to check if it works again 
     Log.d("test", milliseconds + " _ " + System.currentTimeMillis()); 

     //Yet more code 
    } 

    private void playNotification(Context context, String name, String description){ 
     long[] vibrate = new long[2]; 
     vibrate[0] = 500; 
     vibrate[1] = 1000; 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setContentText(description) 
      .setSmallIcon(R.drawable.notificationicon) 
      .setLights(Color.GREEN, 500, 2000) 
      .setAutoCancel(true) 
      .setVibrate(vibrate) 
      .setContentTitle(name); 
     NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     manager.notify(1, builder.build()); 
    } 

} 

我會告訴你,現在,所有的代碼有效。然而,無論出於何種原因,logcat消息和通知都不會出現在屏幕上。 playNotification方法在另一個應用程序中運行良好,所以這不是問題。

這裏的任何想法?

編輯:添加了通知方法的代碼,以防萬一它對任何人都有用。

+0

您是否在測試前至少啓動了一次該應用程序? –

+0

是的,從AndroidStudio運行代碼後,應用程序啓動。我也剛剛打開應用程序,然後重新啓動只是爲了確保和再次,沒有任何反應。 – NuffsaidM8

回答

0

嘗試添加RECEIVE_BOOT_COMPLETED權限。在清單中,在應用程序標記之外添加以下行:

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

謝謝!我在另一個應用程序中有這個,當我轉移東西的時候,我只是無意中跳過了它。 – NuffsaidM8