2017-05-23 6 views
0

AlarmManagerを使用して呼び出すメソッドをスケジュールしようとしていますが、動作していないようです。私は他の例を見てきましたが、それらの例は私のために働いていません。だから私はそれが私のコードから何かだと思っている。AlarmManagerが動作していないようです

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
Intent intent = new Intent(SplashScreenActivity.this, KinectReceiver.class); 

PendingIntent pendingIntent = PendingIntent.getBroadcast(SplashScreenActivity.this, 0, intent, 0); 

Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.set(Calendar.HOUR_OF_DAY, 2); 
calendar.set(Calendar.MINUTE, 25); 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
     calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

そして、私の放送受信機:

public class KinectReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "Done", Toast.LENGTH_LONG).show(); 
     Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
       .setSmallIcon(R.drawable.ic_stat_notify) 
       .setContentTitle("Kinect") 
       .setColor(ContextCompat.getColor(context, R.color.colorAccent)) 
       .setWhen(System.currentTimeMillis()) 
       .setContentText("Your Kinects and Likes have been refilled. Now get to swiping") 
       .setAutoCancel(true) 
       .setSound(uri) 
       .setStyle(new NotificationCompat.BigTextStyle() 
         .bigText("Your Kinects and Likes have been refilled. Now get to swiping")) 
       .setVibrate(new long[] { 100, 500, 100, 500, 100 }); 

     Intent targetIntent = new Intent(context, MainActivity.class); 
     targetIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     targetIntent.putExtra("action", "main"); 
     targetIntent.putExtra("id", ""); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT); 
     builder.setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
     notificationManager.notify(0, builder.build()); 
    } 
} 

トーストや通知のショーのいずれもここでAlarmManagerコードです。

AlarmManagerコードは、私のランチャーアクティビティで実行される最初のものです。おかげ

+0

'KinectReceiver'のマニフェストに' '要素がありますか?また、「HOUR_OF_DAY」は24時間制を基準にしているため、2は2時です。 –

+0

いいえ、ありません。どのように実装するのですか?そして、はい、私は 'Calender'オブジェクトがどのように動作するのか知っています。私はそれを使ってテストしていました。それは私がそれをテストしたときに午前2時だった –

+0

これは大丈夫です: '' –

答えて

1

<receiver 
    android:name=".KinectReceiver"> 
+0

Grearは私のfrd –

+0

を助けるために幸せクイック質問。進行中のアラームスケジュールをキャンセルするにはどうすればよいですか? –

+0

下記のコードを確認してください。私はコメントに投稿できませんので –

1

アプリケーションタグでは、この

ようmanifeastファイルにウルレシーバを登録してください2番目の質問はここに警報

をキャンセルする方法では、ウル答えは

Intent myIntent = new Intent(MainActivity.this, AlarmActivity.class); 
    pendingIntent = PendingIntent.getActivity(CellManageAddShowActivity.this, 
     id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    pendingIntent.cancel(); 
    alarmManager.cancel(pendingIntent); 

ですあなたが必要とする主なものは次のとおりです:

1)同じIDと適切なインテントFLAGで保留中のインテントを作成します。 2)保留中のインテントをキャンセルします。 3)アラームマネージャを使用してアラームをキャンセルします。

+0

これはすべてのアラームをキャンセルします。 –

+0

いいえこれは、同じIDを使用して設定したアラームをキャンセルします。これは、1つのAlaramをスケジュールしました –

+0

すべてのアラームに対して同じIDを使用していれば、すべてをキャンセルします..uは他のPendingIntentフラグも確認できますgoogleの開発者サイト –

関連する問題