你能解释一下为什么这段代码在Android Lollipop上运行良好但我在Android Oreo上运行不好吗?代码设置一个警报,在5秒后开始,每4秒重复一次。它发送广播和MyReceive显示Toast。在android 8中,它开始很晚,下一个广播不会每4秒发送一次,但是在随机时间(最晚)。我能做什么?
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private final String LOG_TEST = "LOG_TEST";
private Button bStart, bStop;
private Intent intent;
private PendingIntent pendingIntent;
public static final String MY_CUSTOM_ACTION = "mio.broadcast";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AlarmManager alarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
intent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent,0);
bStart=(Button)findViewById(R.id.bStart);
bStop= (Button)findViewById(R.id.bStop);
bStart.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Calendar attivazione = Calendar.getInstance();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, attivazione.getTimeInMillis()+5000,4000,pendingIntent);
}
});
bStop.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(alarm!=null){
alarm.cancel(pendingIntent);
}
}
});
}
}
MyReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Vibrazione inserita", Toast.LENGTH_LONG).show();
}
}
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.redwi.alerttry">
<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=".MyReceiver"
android:enabled="true"
android:exported="false" />
</application>
</manifest>
我希望你能帮助我谢谢!
答案 0 :(得分:0)
你能解释一下为什么这段代码在Android Lollipop上运行良好但我在Android Oreo上运行不好吗?
AlarmManager
并非专为经常做事而设计。值得注意的是:
在Android 4.4及更高版本中,setRepeating()
将不准确,这意味着警报可以在此期间的任何地方发生
在Android 5.1及更高版本中,setRepeating()
将被限制为最少一分钟
在Android 6.0及更高版本中,您的闹钟会受到打盹模式的影响,也可能受到应用待机状态的影响,导致它们在设备不在充电器上且不移动时不经常运行
我能做什么?
如果您希望每隔几秒钟显示前景Toast
,请使用postDelayed()
:
/***
Copyright (c) 2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
Covered in detail in the book _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.post;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class PostDelayedDemo extends Activity implements Runnable {
private static final int PERIOD=5000;
private View root=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
root=findViewById(android.R.id.content);
}
@Override
public void onStart() {
super.onStart();
run();
}
@Override
public void onStop() {
root.removeCallbacks(this);
super.onStop();
}
@Override
public void run() {
Toast.makeText(PostDelayedDemo.this, "Who-hoo!", Toast.LENGTH_SHORT)
.show();
root.postDelayed(this, PERIOD);
}
}
答案 1 :(得分:0)
setRepeating
的精确度并不保证准确,并且可能因设备而异,因此无法预测。如果您需要更精确的计划,请使用setExact
并自行重新安排闹钟。来自docs:
注意:从API 19(KITKAT)开始,警报传递是不准确的:操作系统 将移动警报以最小化唤醒和电池使用。那里 是支持需要严格交付的应用程序的新API 担保;请参阅setWindow(int,long,long,PendingIntent)和 setExact(int,long,PendingIntent)。应用程序 targetSdkVersion早于API 19将继续看到 以前的行为,其中所有警报都在何时传递 请求。