Android - 每天特定时间的通知

我正在尝试向我的应用程序添加通知功能。我希望它每天同时运行通知或操作。我现在有这个通知代码:


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.support.v4.app.NotificationCompat;

import android.view.View;


public class MainActivity extends AppCompatActivity {


NotificationCompat.Builder notification;

private static final int uniqueID = 45612;


@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    notification = new NotificationCompat.Builder(this);

    notification.setAutoCancel(true);


    // Build the notification

    notification.setSmallIcon(R.drawable.icon);

    notification.setTicker("Brook Betterment Plan");

    notification.setWhen(System.currentTimeMillis());

    notification.setContentTitle("Brook Betterment Plan");

    notification.setContentText("Don't forget to enter your daily stats! ");


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

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setContentIntent(pendingIntent);


    // Builds notification and issues it

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    nm.notify(uniqueID, notification.build());

}

}

谢谢!希望有人知道答案。另外,我希望它不需要任何额外的 Activity 或 Java 类。


一只斗牛犬
浏览 175回答 2
2回答

SMILET

创建一个将生成通知的类,如 LocalNotification在类似 showDailyNotification 的方法下添加您在该类中创建的代码现在使用 JobScheduler 并每 24 小时安排一次作业,当作业开始时在 LocalNotification 中调用此方法使 LocalNotification 成为单例类。顺便说一句,不要忘记使用通知渠道,否则它将无法在奥利奥及以上设备上运行。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java