MainActivity
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.util.Log; import android.view.View; import android.widget.Button; import com.example.testnotificationmanagerrepeat.R; import java.util.Calendar; public class MainActivity extends AppCompatActivity { private AlarmManager am; private PendingIntent pending; private int requestCode = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button buttonStart = this.findViewById(R.id.button1); buttonStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); // 10sec calendar.add(Calendar.SECOND, 10); Intent intent = new Intent(getApplicationContext(), AlarmNotification2.class); intent.putExtra("RequestCode",requestCode); pending = PendingIntent.getBroadcast( getApplicationContext(),requestCode, intent, 0); // アラームをセットする am = (AlarmManager) getSystemService(ALARM_SERVICE); if (am != null) { am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending); } } }); } }
AlarmNotification2
import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.WallpaperInfo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.media.RingtoneManager; import android.net.Uri; import android.util.Log; import com.example.testnotificationmanagerrepeat.R; import java.text.SimpleDateFormat; import java.util.Locale; public class AlarmNotification2 extends BroadcastReceiver { private WallpaperInfo getApplicationContext; @Override // データを受信した public void onReceive(Context context, Intent intent) { Log.d("AlarmBroadcastReceiver","onReceive() pid=" + android.os.Process.myPid()); int requestCode = intent.getIntExtra("RequestCode",0); PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); String channelId = "default"; // app name String title = context.getString(R.string.abc); long currentTime = System.currentTimeMillis(); SimpleDateFormat dataFormat = new SimpleDateFormat("HH:mm:ss", Locale.JAPAN); String cTime = dataFormat.format(currentTime); // メッセージ + 11:22:331 String message = "あくしろごみ "+cTime ; NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // Notification Channel 設定 NotificationChannel channel = new NotificationChannel( channelId, title , NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription(message); channel.enableVibration(true); channel.canShowBadge(); channel.enableLights(true); channel.setLightColor(Color.BLUE); // the channel appears on the lockscreen channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); channel.setSound(defaultSoundUri, null); channel.setShowBadge(true); if(notificationManager != null){ notificationManager.createNotificationChannel(channel); Notification notification = new Notification.Builder(context, channelId) .setContentTitle(title) // android標準アイコンから .setSmallIcon(R.drawable.mamedennkyuu) .setContentText(message) .setAutoCancel(true) .setContentIntent(pendingIntent) .setWhen(System.currentTimeMillis()) .build(); // 通知 notificationManager.notify(R.string.app_name, notification); } }
あなたの回答
tips
プレビュー