根据时间发送通知 Android [英] Send Notification based on time Android
本文介绍了根据时间发送通知 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Android 开发新手,我需要根据 2 种情况发送通知:
- 使用showNotification"方法点击showNotification"按钮
- 一段时间后(在我的示例中单击alertNotification"按钮后 5 秒)使用alertNotification"方法如下:
第一个案例效果很好.但是,我遇到了第二种情况的问题.
我遵循了在 https://youtu.be/gm5n_hRIR-c 上解释的相同方法 >
这是我的代码:
MainActivity.java
public class MainActivity extends Activity {//点击按钮时发送通知按钮显示通知;//点击按钮后 5 秒发送通知按钮警报通知;通知管理器通知管理器;boolean isNotificActive = false;int 通知 ID = 33;public static final int NOTIFICATION_ID = 1;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.sample_layout);showNotification = (Button) findViewById(R.id.showNotification);alertNotification = (Button) findViewById(R.id.alerNotification);}public void showNotification(查看视图){NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setContentTitle("通知").setContentText("新通知").setTicker("提醒新通知").setSmallIcon(R.drawable.ic_stat_notification);Intent moreInfoIntent = new Intent(this, MoreNotification.class);TaskStackBuilder stackBuilder = null;如果 (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {stackBuilder = TaskStackBuilder.create(this);stackBuilder.addParentStack(MoreNotification.class);stackBuilder.addNextIntent(moreInfoIntent);PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);notificationBuilder.setContentIntent(pendingIntent);NotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);notificationManager.notify(notifID, notificationBuilder.build());isNotificActive = true;}}公共无效警报通知(查看视图){Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;意图 alertIntent = new Intent(this, AlertReceiver.class);AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);警报管理器.设置(警报管理器.RTC_WAKEUP,警报时间,PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));Log.e("alarmManager", "alarmManager");}}
AlertReceiver.java
public class AlertReceiver extends BroadcastReceiver {@覆盖public void onReceive(上下文上下文,意图意图){Log.e("onReceive", "onReceive");createNotification(context, "Times UP", "5 Seconds Has Passed", "Alert");}公共无效创建通知(上下文上下文,字符串味精,字符串味精文本,字符串味精警报){PendingIntent notificationIntent = PendingIntent.getActivity(context, 0,新意图(上下文,MainActivity.class),0);NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_stat_notification).setContentTitle(味精).setTicker(msgAlert).setContentText(msgText);mBuilder.setContentIntent(notificationIntent);mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);mBuilder.setAutoCancel(true);NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);mNotificationManager.notify(1, mBuilder.build());}}
AndroidManifest.xml
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/><申请机器人:allowBackup =真"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme"><活动android:name=".MainActivity"android:label="@string/app_name"><意图过滤器><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></意图过滤器></活动><活动android:name=".MoreNotification"android:label="更多通知"android:parentActivityName=".MainActivity"><元数据android:name="android.support.PARENT_ACTIVITY"android:value=".MainActivity"/></活动></应用程序>
解决方案
AlertReceiver
必须在 AndroidManifest.xml
中注册.
<应用程序机器人:allowBackup =真"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme"><!-- 你的东西在这里.--><receiver android:name=".AlertReceiver"/></应用程序>
I am new to Android development, and I need to send a notification based on 2 cases:
- On "showNotification" Button Click using "showNotification" Method
- After a period of time (5 seconds after "alertNotification" button clicked in my example) using "alertNotification" method as below:
The first case has worked perfect. But, I am having a problem with the Second case.
I followed the same way explained on https://youtu.be/gm5n_hRIR-c
Here are my Code:
MainActivity.java
public class MainActivity extends Activity { // Send Notification on button click Button showNotification; // Send Notification 5 second after on button click Button alertNotification; NotificationManager notificationManager; boolean isNotificActive = false; int notifID = 33; public static final int NOTIFICATION_ID = 1; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sample_layout); showNotification = (Button) findViewById(R.id.showNotification); alertNotification = (Button) findViewById(R.id.alerNotification); } public void showNotification(View view) { NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setContentTitle("Notification") .setContentText("New Notification") .setTicker("Alert New Notification") .setSmallIcon(R.drawable.ic_stat_notification); Intent moreInfoIntent = new Intent(this, MoreNotification.class); TaskStackBuilder stackBuilder = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(MoreNotification.class); stackBuilder.addNextIntent(moreInfoIntent); PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent. FLAG_UPDATE_CURRENT); notificationBuilder.setContentIntent(pendingIntent); notificationManager = (NotificationManager) getSystemService(Context. NOTIFICATION_SERVICE); notificationManager.notify(notifID, notificationBuilder.build()); isNotificActive = true; } } public void alertNotification(View view) { Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000; Intent alertIntent = new Intent(this, AlertReceiver.class); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast( this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT)); Log.e("alarmManager", "alarmManager"); } }
AlertReceiver.java
public class AlertReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.e("onReceive", "onReceive"); createNotification(context, "Times UP", "5 Seconds Has Passed", "Alert"); } public void createNotification(Context context, String msg, String msgText, String msgAlert) { PendingIntent notificationIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_stat_notification) .setContentTitle(msg) .setTicker(msgAlert) .setContentText(msgText); mBuilder.setContentIntent(notificationIntent); mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND); mBuilder.setAutoCancel(true); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService( Context.NOTIFICATION_SERVICE); mNotificationManager.notify(1, mBuilder.build()); } }
AndroidManifest.xml
<!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle --> <uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MoreNotification" android:label="More Notification" android:parentActivityName=".MainActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/> </activity> </application>
解决方案
AlertReceiver
must be registered in AndroidManifest.xml
.
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<!-- Your stuff here. -->
<receiver android:name=".AlertReceiver"/>
</application>
这篇关于根据时间发送通知 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文