2017-09-01 12 views
0

私はアンドロイドの新機能です。私は、特定の日時(たとえば、10月9日9月9日の日付10:00 AM)の通知を私のアプリで表示したいと思います。アラームマネージャを使用していますが、正常に動作しません。タスクスケジューラを使用して特定の日時の通知を表示

適切な方法を教えてください。 MainActivity

パブリッククラスMainActivityアクティビティを拡張{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.YEAR, 2017); 
    calendar.set(Calendar.MONTH, 9); 
    calendar.set(Calendar.DAY_OF_MONTH, 9); 
    calendar.set(Calendar.HOUR_OF_DAY, 10); 
    calendar.set(Calendar.MINUTE, 0); 
    calendar.set(Calendar.SECOND, 0); 

    Intent inn = new Intent(MainActivity.this,AlarmActivity.class); 
    PendingIntent displayIntent = PendingIntent.getActivity(
      getBaseContext(), 0,inn, 0); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, 
      calendar.getTimeInMillis(), displayIntent); 

} 

AlarmActivity

パブリッククラスAlarmActivityアクティビティ {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.content_alarm); 

    final Intent notificationIntent = new Intent(this, AlarmActivity.class); 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent contentIntent = PendingIntent.getActivity(AlarmActivity.this, 0, notificationIntent, 0); 

    Notification notification1 = new Notification.Builder(AlarmActivity.this) 
      .setContentTitle("Notification") 
      .setContentText("Get Notigication") 
      .setContentIntent(contentIntent).setWhen(System.currentTimeMillis()) 
      .setOngoing(true) 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .build(); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    notificationManager.notify(1, notification1); 
} 
を拡張

マニフェスト 許可

+0

あなたが何かをしようとしたことがありますか?あなたのコードで質問を更新してください – Yannick

+0

アプリケーショントレイからアプリを削除すると、すべての電話で機能しません。アプリにサービスを提供します – phpdroid

答えて

0

アラームをトリガーするには、アクティビティで通知を生成するサービスをトリガーする場所からWakefulBroadcastReceiverを使用する必要があります。

public class AlarmReceiver extends WakefulBroadcastReceiver { 

@Override 
public void onReceive(final Context context, Intent intent) { 

    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri); 
    ringtone.play(); 
    ComponentName comp = new ComponentName(context.getPackageName(), 
      AlarmService.class.getName()); 
    startWakefulService(context, (intent.setComponent(comp))); 
    setResultCode(Activity.RESULT_OK); 
} 

}

次に、あなたがこのようなサービスを作成します。

public class AlarmService extends IntentService { 


public AlarmService() { 
    super("AlarmService"); 
} 


@Override 
public void onHandleIntent(Intent intent) { 
    contentId = intent.getStringExtra("id"); 
    String title = intent.getStringExtra("title"); 
    String subtitle = intent.getStringExtra("subtitle"); 
    sendNotification(title, subtitle); 
} 
    public void sendNotification(String title, String subtitle) { 


    Notification notification = new Notification.Builder(this) 
      .setSmallIcon(R.drawable.notification_icon) 
      .build(); 

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.reminder_notification_view); 
    contentView.setInt(R.id.layout, "setBackgroundColor", 
      Color.WHITE); 
    contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher); 
    contentView.setTextViewText(R.id.title, title); 
    contentView.setTextViewText(R.id.text, subtitle); 
    notification.contentView = contentView; 

    Intent intent = new Intent(this, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

    notification.contentIntent = PendingIntent.getActivity(this, someRandomId, 
      intent, 0); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; //Do not clear the notification 
    notification.defaults |= Notification.DEFAULT_LIGHTS; // LED 
    notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration 
    notification.defaults |= Notification.DEFAULT_SOUND; // Sound 

    mNotificationManager.notify(someRandomId, notification); 

} 

}

+0

DLConstantsとは何ですか? – SACHIN

+0

バンドルは無視できます。私は答えを編集しています。 –

関連する問題