如何在每48小时后显示android通知?

我尝试了以下代码Android通知,这是正常工作,但它是在我启动我的Android应用程序时给我通知弹出窗口.

I want to show an notification after every 48hours,how can I do it?

为了实现这一点,我需要做出哪些改变?

通知代码

Intent notificationIntent = new Intent(MainActivity.this,Activity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,notificationIntent,0);

         notificationmanager notificationmanager = (notificationmanager) MainActivity.this
                    .getSystemService(Context.NOTIFICATION_SERVICE);

         Notification noti = new NotificationCompat.Builder(MainActivity.this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setTicker("ticker message")

                            .setWhen(System.currentTimeMillis())
                            .setContentTitle("HELLO")
                            .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS")
                            .setContentIntent(contentIntent)
                            //At most three action buttons can be added
                            .setAutoCancel(true).build();
        int notifyID =0;
        notificationmanager.notify(notifyID,noti);

解决方法

这是您在主要活动中所需要的东西:
Intent notificationIntent = new Intent(context,ShowNotification.class);
    PendingIntent contentIntent = PendingIntent.getService(context,PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.cancel(contentIntent);
    am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()
            + AlarmManager.INTERVAL_DAY * 2,AlarmManager.INTERVAL_DAY * 2,contentIntent);

然后,在另一个名为ShowNotification.java的文件中,添加以下内容(假设您的主要活动名为MainActivity):

import android.app.Notification;
import android.app.notificationmanager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class ShowNotification extends Service {

    private final static String TAG = "ShowNotification";

    @Override
    public void onCreate() {
        super.onCreate();

        Intent mainIntent = new Intent(this,MainActivity.class);

        notificationmanager notificationmanager
            = (notificationmanager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification noti = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentIntent(PendingIntent.getActivity(this,mainIntent,PendingIntent.FLAG_UPDATE_CURRENT))
            .setContentTitle("HELLO " + System.currentTimeMillis())
            .setContentText("PLEASE CHECK WE HAVE UPDATED NEWS")
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.ic_launcher)
            .setTicker("ticker message")
            .setWhen(System.currentTimeMillis())
            .build();

        notificationmanager.notify(0,noti);

        Log.i(TAG,"Notification created");
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Todo Auto-generated method stub
        return null;
    }
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...