您好我在uni中有一个项目,我需要向我的应用程序添加通知。这是创建和启动通知的代码。一切都编译良好,但它不显示任何通知。 报警接收器 -
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);
}
}
}
答案 0 :(得分:1)
在你的主要活动中把这个:
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 = 2*60*1000; // time interval in ms, 2 min in this case
// 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 :(得分:0)
此代码将每隔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());
}
}
将此活动添加为启动器活动 -
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);
}
}
不要忘记将接收器注册到您的清单文件 -
<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>