Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每五分钟显示一次android通知_Android_Notifications - Fatal编程技术网

每五分钟显示一次android通知

每五分钟显示一次android通知,android,notifications,Android,Notifications,我想知道如何设置通知时间。我想每五分钟通知一次, 帮我做吧 public class FirstActivity extends Activity { private static final int HELLO_ID = 1; //public static final int FLAG_AUTO_CANCEL = 0;`enter code here` @Override protected void onCreate(Bundle savedInstance

我想知道如何设置通知时间。我想每五分钟通知一次, 帮我做吧

public class FirstActivity extends Activity
{
    private static final int HELLO_ID = 1;
    //public static final int FLAG_AUTO_CANCEL = 0;`enter code here`
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstactivity);

        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.icon;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();

        Notification notification = new Notification(icon, tickerText, when);

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.statusbarnotification);
        contentView.setImageViewResource(R.id.image, R.drawable.icon);
        contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");
        notification.contentView = contentView;
        notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
        Intent notificationIntent = new Intent(getApplicationContext(), SecondActivity.class);
        //PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.contentIntent = contentIntent;
        mNotificationManager.notify(HELLO_ID, notification);
    }

}

我认为最好的方法是创建一个设置通知的服务,然后使用AlarmManager激活该服务。
这是AlarmManager的代码:

private void startAlarm() {
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
    long when = System.currentTimeMillis();         // notification time
    Intent intent = new Intent(this, ReminderService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC, when, (AlarmManager.INTERVAL_FIFTEEN_MINUTES / 3), pendingIntent);
}
要控制时间间隔,请使用常量值或插入自己的值(以毫秒为单位)

服务内容如下:

public class ReminderService extends IntentService {
    private static final int NOTIF_ID = 1;

    public ReminderService(){
        super("ReminderService");
    }

    @Override
      protected void onHandleIntent(Intent intent) {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        long when = System.currentTimeMillis();         // notification time
        Notification notification = new Notification(R.drawable.icon, "reminder", when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.flags |= notification.FLAG_AUTO_CANCEL;
        Intent notificationIntent = new Intent(this, YourActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
        notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
        nm.notify(NOTIF_ID, notification);
    }

}

谢谢是我想要的,但我有个问题要问你。如何在服务类中设置
AlarmManager
,以便在它检查数据库并有新内容时显示通知?(如果您可以用代码解释会更好,不需要数据库的代码…只有服务类中带有
If
语句的AlarmManager)调用
StartAlarm()
时??我没有收到任何通知。。代码不工作:(