亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android - 每天特定時間的通知

Android - 每天特定時間的通知

一只斗牛犬 2021-06-01 16:57:51
我正在嘗試向我的應用程序添加通知功能。我希望它每天同時運行通知或操作。我現在有這個通知代碼: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 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

創建一個將生成通知的類,如 LocalNotification

在類似 showDailyNotification 的方法下添加您在該類中創建的代碼

現在使用 JobScheduler 并每 24 小時安排一次作業,當作業開始時在 LocalNotification 中調用此方法

使 LocalNotification 成為單例類。

順便說一句,不要忘記使用通知渠道,否則它將無法在奧利奧及以上設備上運行。


查看完整回答
反對 回復 2021-06-10
  • 2 回答
  • 0 關注
  • 189 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號