2016-04-28 102 views
0

我正在開發一個應用程序,當鬧鐘被觸發時,它會在後臺執行某些操作。現在它只是一個敬酒。 運行時,我沒有得到任何錯誤,但當警報響起時,廣播接收器不會被調用。 我錯過了什麼?當有鬧鐘時,BroadcastReceiver不會觸發

AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver { 
     public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT"; 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      { 
       Intent alarm = new Intent(ALARM_ALERT_ACTION); 
       // context.registerReceiver(this, alarm); 
       String action = intent.getAction(); 
       if (action.equals(ALARM_ALERT_ACTION)) 
       { 
        // for play/pause mediaplayer 
        CharSequence text = "Hello toast!"; 
        int duration = Toast.LENGTH_SHORT; 

        Toast toast = Toast.makeText(context, text, duration); 
        toast.show(); 
       } 
      } 
     } 
    } 

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.noel.alarmnotification"> 
    <uses-permission android:name="com.android.alarm.permission."/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <receiver android:name=".AlarmReceiver" android:process=":remote"> 
       <intent-filter> 
        <action android:name="com.android.deskclock.ALARM_ALERT"/> 
       </intent-filter> 
      </receiver> 
     </activity> 
    </application> 

</manifest> 

EDIT

MainActivity:

public class MainActivity extends AppCompatActivity { 
    public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT"; 
    public static final String ALARM_SNOOZE_ACTION = "com.android.deskclock.ALARM_SNOOZE"; 
    public static final String ALARM_DISMISS_ACTION = "com.android.deskclock.ALARM_DISMISS"; 
    public static final String ALARM_DONE_ACTION = "com.android.deskclock.ALARM_DONE"; 


    private BroadcastReceiver mReceiver = new BroadcastReceiver() 
    { 
     @Override 
     public void onReceive(Context context, Intent intent) 
     { 
      String action = intent.getAction(); 
      if (action.equals(ALARM_ALERT_ACTION) || action.equals(ALARM_DISMISS_ACTION) || action.equals(ALARM_SNOOZE_ACTION) || action.equals(ALARM_DONE_ACTION)) 
      { 
       // for play/pause mediaplayer 

      } 
     } 
    }; 


    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     IntentFilter filter = new IntentFilter(ALARM_ALERT_ACTION); 
     filter.addAction(ALARM_DISMISS_ACTION); 
     filter.addAction(ALARM_SNOOZE_ACTION); 
     filter.addAction(ALARM_DONE_ACTION); 
     registerReceiver(mReceiver, filter); 
    } 
} 

瓦時恩我編程方式創建一個寄存器,一旦應用程序被關閉,它不廣播報警

+0

當你聽到廣播接收器沒有被叫做***時,你的意思是什麼?您會聽到警報,但接收器內的方法不會被執行? – Opiatefuchs

+0

是的,這就是發生了什麼,我調試它,但程序永遠不會到達廣播接收器內的斷點 – programernoob

+0

我不太確定,但我想你需要的權限'<使用權限android:name =「com.android.alarm .permission.SET_ALARM「/>'。如果你是在棉花糖行事,你也需要運行時的權限請求。 – Opiatefuchs

回答