2016-04-28 12 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); 
    } 
} 

Whを私はプログラム的にレジスタを作成途中のアプリが閉じられると、それは次のスレッドがあなたのために役立つかもしれないアラーム

+0

アラーム音が鳴ったら放送受信機は***と呼ばれません。アラームが聞こえますが、受信機内部のメソッドは実行されませんか? – Opiatefuchs

+0

はい、それは起こります、私はそれをデバッグしますが、プログラムはbroadcastreceiver内のブレークポイントに達することはありません – programernoob

+0

私は本当にそれについてはわかりませんが、あなたは許可を必要とすると思う 'use-permission android:name =" com.android.alarm .permission.SET_ALARM "/>'。そして、あなたがマシュマロに行動しているなら、実行時に許可要求も必要です。 – Opiatefuchs

答えて

関連する問題