奥利奥在一小时内安排通知
Oreo Scheduling a Notification in an hour
在 Android Oreo 中,我正在尝试在已知的未来时间安排通知。我写了一小段代码来测试我是否可以在应用程序可能关闭的一小时内安排通知。当我尝试这样做时,没有任何反应。非常感谢您的帮助。
public class MainActivity extends AppCompatActivity {
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationChannel notificationChannel = new NotificationChannel("default",
"primary", NotificationManager.IMPORTANCE_DEFAULT);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
Intent notifyIntent = new Intent(this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
long milisInFuture = 1000 * 60 * 60;
alarmManager.set(AlarmManager.RTC_WAKEUP, milisInFuture, pendingIntent);
}
public class MyNewIntentService extends JobIntentService {
@Override
protected void onHandleWork(@NonNull Intent intent) {
Notification notification = new Notification.Builder(getApplicationContext(), "default")
.setContentTitle("title")
.setContentText("body")
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setAutoCancel(true)
.build();
manager.notify(123, notification);
}
}
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, MyNewIntentService.class);
context.startService(intent1);
}
}
}
如果有帮助,这是清单。
<?xml version="1.0" encoding="utf-8"?>
<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>
<service
android:name=".MainActivity$MyNewIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"></service>
</application>
</manifest>
更新:
不要忘记在清单 xml 文件中添加接收器。
对于这个问题:
在AndroidManifest.xml内添加应用标签:
<receiver android:name=".MyReceiver" />
示例:
我保留了这个例子,它更新了来自 Broadcast Receiver class.
的通知
如图所示创建广播接收器 class,(不要忘记在清单 xml 文件中添加接收器)
public class NotificationUpdate extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
//NEED A RESCHEDULE?
updateNotification(context);
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void updateNotification(Context context){
Notification notification = new Notification.Builder(context.getApplicationContext(), "default")
.setContentTitle("title")
.setContentText("body")
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setAutoCancel(true)
.build();
NotificationManager manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
if(manager!=null) manager.notify(123, notification);
}
}
来自 Activity 的示例调用:
public class MainActivity extends AppCompatActivity {
final long intervalPeriod=60*1000;
AlarmManager mAlarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationChannel notificationChannel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel("default",
"primary", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) manager.createNotificationChannel(notificationChannel);
mAlarmManager=(AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE);
PendingIntent intent=PendingIntent.getBroadcast(getApplicationContext(),1234,
new Intent(getApplicationContext(),NotificationUpdate.class),0);
mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+intervalPeriod, intent);
}
}
在 Android Oreo 中,我正在尝试在已知的未来时间安排通知。我写了一小段代码来测试我是否可以在应用程序可能关闭的一小时内安排通知。当我尝试这样做时,没有任何反应。非常感谢您的帮助。
public class MainActivity extends AppCompatActivity {
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationChannel notificationChannel = new NotificationChannel("default",
"primary", NotificationManager.IMPORTANCE_DEFAULT);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
Intent notifyIntent = new Intent(this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
long milisInFuture = 1000 * 60 * 60;
alarmManager.set(AlarmManager.RTC_WAKEUP, milisInFuture, pendingIntent);
}
public class MyNewIntentService extends JobIntentService {
@Override
protected void onHandleWork(@NonNull Intent intent) {
Notification notification = new Notification.Builder(getApplicationContext(), "default")
.setContentTitle("title")
.setContentText("body")
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setAutoCancel(true)
.build();
manager.notify(123, notification);
}
}
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, MyNewIntentService.class);
context.startService(intent1);
}
}
}
如果有帮助,这是清单。
<?xml version="1.0" encoding="utf-8"?>
<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>
<service
android:name=".MainActivity$MyNewIntentService"
android:permission="android.permission.BIND_JOB_SERVICE"></service>
</application>
</manifest>
更新:
不要忘记在清单 xml 文件中添加接收器。 对于这个问题:
在AndroidManifest.xml内添加应用标签:
<receiver android:name=".MyReceiver" />
示例: 我保留了这个例子,它更新了来自 Broadcast Receiver class.
的通知如图所示创建广播接收器 class,(不要忘记在清单 xml 文件中添加接收器)
public class NotificationUpdate extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
//NEED A RESCHEDULE?
updateNotification(context);
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void updateNotification(Context context){
Notification notification = new Notification.Builder(context.getApplicationContext(), "default")
.setContentTitle("title")
.setContentText("body")
.setSmallIcon(android.R.drawable.stat_notify_chat)
.setAutoCancel(true)
.build();
NotificationManager manager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
if(manager!=null) manager.notify(123, notification);
}
}
来自 Activity 的示例调用:
public class MainActivity extends AppCompatActivity {
final long intervalPeriod=60*1000;
AlarmManager mAlarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NotificationChannel notificationChannel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel("default",
"primary", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) manager.createNotificationChannel(notificationChannel);
mAlarmManager=(AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE);
PendingIntent intent=PendingIntent.getBroadcast(getApplicationContext(),1234,
new Intent(getApplicationContext(),NotificationUpdate.class),0);
mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+intervalPeriod, intent);
}
}