我想为我的Android应用创建一个简单的推送通知(每天下午6点重复)
我做了一些搜索,发现:How To give notifications on android on specific time?
我遵循了那个教程,这是我到目前为止所得到的:
public class MyReceiver extends BroadcastReceiver
{
public MyReceiver()
{
}
@Override
public void onReceive(Context context, Intent intent)
{
Intent intent1 = new Intent(context, HistoryToday.class);
context.startService(intent1);
}
}
public class MyNewIntentService extends IntentService
{ private static final int NOTIFICATION_ID = 3;
public MyNewIntentService() {
super("MyNewIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("My Titel");
builder.setContentText("This is the Body");
builder.setSmallIcon(R.mipmap.logo_app_icon);
Intent notifyIntent = new Intent(this, HistoryToday.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//to be able to launch your activity from the notification
builder.setContentIntent(pendingIntent);
Notification notificationCompat = builder.build();
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
managerCompat.notify(NOTIFICATION_ID, notificationCompat);
}
}
清单
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false" >
</receiver>
<service
android:name=".MyNewIntentService"
android:exported="false" >
</service>
Java类
private void Intend()
{
Context context = this.getApplicationContext();
Intent notifyIntent = new Intent(this,MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast
(context, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
10000, pendingIntent);
}
抱歉格式错误,我希望它是可读的
未显示任何通知:-( 也许有人可以帮助我。谢谢大家!
答案 0 :(得分:0)
尝试使用AlarmManager,这是我可以根据您的要求向您推荐的最佳选择。检查此代码:
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 18);
cal.set(Calendar.MINUTE, 00);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(ProfileList.this,
IntentBroadcastedReceiver.class);
PendingIntent pintent = PendingIntent.getService(ProfileList.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
此代码将每天下午6点注册该活动。现在你必须通过添加广播接收器来捕获事件。这是代码:
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
// Generate a push notification here.
}
}
不要忘记在Manifest中定义广播接收器:
<receiver android:name=".MyBroadcastReceiver" >