Android 简单重复推送通知
我想为我的Android应用程序创建一个简单的推送通知(每天下午6点重复) 我做了一些搜索,发现: 我遵循了该教程,以下是我到目前为止的收获:Android 简单重复推送通知,android,Android,我想为我的Android应用程序创建一个简单的推送通知(每天下午6点重复) 我做了一些搜索,发现: 我遵循了该教程,以下是我到目前为止的收获: public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { Intent intent1 = new Intent(con
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
{
私有静态最终整数通知_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>
对不起,格式不好,我希望它是可读的
未显示任何通知:-(
也许有人可以帮我。谢谢大家!试试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.
}
}
不要忘记在清单中定义广播接收器:
<receiver android:name=".MyBroadcastReceiver" >
<receiver android:name=".MyBroadcastReceiver" >