gpt4 book ai didi

java - 每 15 分钟创建一次 Android 通知

转载 作者:行者123 更新时间:2023-12-02 13:38:12 24 4
gpt4 key购买 nike

您好,我在大学有一个项目,我需要向我的应用程序添加通知。这是创建和启动通知的代码。一切都编译良好,但它不显示任何通知。 警报接收器 -

package com.example.alex.daily_horoscope;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
// fire notification

// Gets an instance of the NotificationManager service
final NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(context.getResources().getText(R.string.notification))
.setContentText("You have new horoscope");
// This pending intent will open after notification click
Intent resultIntent = new Intent(context, MainActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(context, 0, resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
// Sets an ID for the notification
int mNotificationId = 001;
// Builds the notification and issues it.
mgr.notify(mNotificationId, mBuilder.build());
}
}

服务启动器

package com.example.alex.daily_horoscope;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

public class ServiceStarter extends BroadcastReceiver {
public final static int ALARM_ID = 12345;

@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Start service");
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
System.out.println("Boot completed");
// start the alarm on phone reboot
Intent intentAlarm = new Intent(context, AlarmReceiver.class);
System.out.println("calling Alarm receiver ");
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//set the notification to repeat every fifteen minutes
long startTime = 1*60*1000; // 2 min
// set unique id to the pending item, so we can call it when needed
PendingIntent pi = PendingIntent.getBroadcast(context, ALARM_ID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() +
startTime, AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);

}

}

}

最佳答案

此代码将每隔 1 分钟向您显示一条通知。

用此替换您的 AlarmReceiver 类 -

public class AlarmReceiver extends BroadcastReceiver {
int mNotificationId = 001;
public AlarmReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "AlarmReceiver", Toast.LENGTH_LONG).show();
// Gets an instance of the NotificationManager service
final NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);

mBuilder.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle(context.getResources().getText(R.string.notification))
.setContentText("You have new horoscope")
.setAutoCancel(true); // clear notification after click

Intent resultIntent = new Intent(context, MainActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(context, mNotificationId, resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mNotificationId, mBuilder.build());
}
}

将此 Activity 添加为启动器 Activity -

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Intent intentAlarm = new Intent(this, AlarmReceiver.class);
System.out.println("calling Alarm receiver ");
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
//set the notification to repeat every fifteen minutes
long startTime = 1*60*1000; // 2 min
// set unique id to the pending item, so we can call it when needed
PendingIntent pi = PendingIntent.getBroadcast(this, ALARM_ID, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.RTC, SystemClock.elapsedRealtime() +
startTime, 60*1000, pi);

}
}

不要忘记将接收器注册到您的 list 文件中--

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shajib.customadapter">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AlarmReceiver">
</receiver>

</application>

</manifest>

关于java - 每 15 分钟创建一次 Android 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42881183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com