我正在尝试向我的应用程序添加通知功能。我希望它每天同时运行通知或操作。我现在有这个通知代码: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;@Overrideprotected 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 类。
2 回答
![?](http://img1.sycdn.imooc.com/5333a0aa000121d702000200-100-100.jpg)
SMILET
TA贡献1796条经验 获得超4个赞
创建一个将生成通知的类,如 LocalNotification
在类似 showDailyNotification 的方法下添加您在该类中创建的代码
现在使用 JobScheduler 并每 24 小时安排一次作业,当作业开始时在 LocalNotification 中调用此方法
使 LocalNotification 成为单例类。
顺便说一句,不要忘记使用通知渠道,否则它将无法在奥利奥及以上设备上运行。
添加回答
举报
0/150
提交
取消