安卓 - 每天特定时间的通知

3
我将尝试为我的应用程序添加一个通知功能。我希望它每天同时运行一次通知或操作。目前,这是我的通知代码:
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类。

2个回答

4

Alarm Manager

public static void startAlarmBroadcastReceiver(Context context) {
    Intent _intent = new Intent(context, AlarmBroadcastReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, 0);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 0);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}

警报广播接收器

AndroidManifest文件中,只需要定义这个类:

<receiver android:name=".AlarmBroadcastReceiver" >
</receiver>

代码将会像这样

public class AlarmBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    showNotification(context);

}

void showNotification(Context context) {
    String CHANNEL_ID = "your_name";// The id of the channel.
    CharSequence name = context.getResources().getString(R.string.app_name);// The user-visible name of the channel.
    NotificationCompat.Builder mBuilder;
    Intent notificationIntent = new Intent(context, TestActivity.class);
    Bundle bundle = new Bundle();
    notificationIntent.putExtras(bundle);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= 26) {
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
        mNotificationManager.createNotificationChannel(mChannel);
        mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLights(Color.RED, 300, 300)
                .setChannelId(CHANNEL_ID)
                .setContentTitle("Title");
    } else {
        mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentTitle("Title");
    }

    mBuilder.setContentIntent(contentIntent);
    mBuilder.setContentText("Your Text");
    mBuilder.setAutoCancel(true);
    mNotificationManager.notify(1, mBuilder.build());
}

}


我认为没有必要检查Android操作系统的版本。只需为所有Android设备使用渠道即可。 - Kwnstantinos Nikoloutsos
1
谢谢!很多答案对我都不起作用,但这个非常清晰,而且像魔法一样有效。太棒了! - MuSoundiX
如何使闹钟在应用程序关闭时仍然能够响起? - Lidor Eliyahu Shelef

0
创建一个类,用于生成通知,就像LocalNotification一样。
在该类中添加您在onCreate中拥有的代码,放在一个名为showDailyNotification的方法下面。
现在使用JobScheduler并安排每24小时执行一次任务,当任务启动时,在LocalNotification中调用此方法。
将LocalNotification设置为单例类。
顺便提一句,请不要忘记使用通知通道,否则它将无法在Oreo及以上设备上工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接