我目前正在尝试使用AlarmManager调用BroadCastReceiver来在一定时间内输出通知!在Android SDK Level 19+上我的方法有效,但在Android 17上我的方法一无所获!
没有输出错误,Android Studio的显示器中也没有任何日志。 AlarmManager几乎不会调用BroadcastReceiver。
MainActivity.java
plo = newdat$distance-cmult*sqrt(pvar1)
NotificationPublisher.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setNotification();
}
});
}
public void setNotification() {
Intent resultIntent = new Intent(this, NotificationPublisher.class);
resultIntent.setData(Uri.parse("notification://1995-" + System.currentTimeMillis()));
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent resultPendingIntent =
PendingIntent.getBroadcast(
this,
1995,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 30 * 1000, resultPendingIntent);
Log.d("Test", "Alarm started");
}
}
AndroidManifext.xml
public class NotificationPublisher extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "What", Toast.LENGTH_SHORT).show();
}
}
Min SDK:17 目标:23
编辑: 我用“adb shell dumpsys alarm”查看了AlarmManager: 警报由应用程序创建,但警报的“when”变量从+30秒倒计时到无限负数!