Android 报警管理器在使用中异常重复
我创建了一个简单的服务,它必须每秒钟记录一次日志。我使用了alarm manager(因为上面说的是Android 报警管理器在使用中异常重复,android,alarmmanager,Android,Alarmmanager,我创建了一个简单的服务,它必须每秒钟记录一次日志。我使用了alarm manager(因为上面说的是注意:alarm manager适用于您希望在特定时间运行应用程序代码的情况,即使您的应用程序当前未运行。),但它会随机重复。 这是我的密码 mainActivity.class public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedI
注意:alarm manager适用于您希望在特定时间运行应用程序代码的情况,即使您的应用程序当前未运行。
),但它会随机重复。
这是我的密码
mainActivity.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
if (fab != null) {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,LogService.class);
startService(intent);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class LogService extends Service {
private static final String TAG = LogService.class.getSimpleName();
public LogService() {
super();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand: Service calling" );
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onCreate() {
Timer(this);
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void Timer(Context context){
Intent intent = new Intent(context,LogService.class);
PendingIntent pendingIntent = PendingIntent.getService(context,0,intent,0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getApplicationContext().ALARM_SERVICE);
alarmManager.setRepeating(alarmManager.RTC_WAKEUP,System.currentTimeMillis(),1*1000,pendingIntent);
}
}
我通过点击FAB按钮启动服务
LogService.class
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
if (fab != null) {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,LogService.class);
startService(intent);
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
public class LogService extends Service {
private static final String TAG = LogService.class.getSimpleName();
public LogService() {
super();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand: Service calling" );
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onCreate() {
Timer(this);
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void Timer(Context context){
Intent intent = new Intent(context,LogService.class);
PendingIntent pendingIntent = PendingIntent.getService(context,0,intent,0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getApplicationContext().ALARM_SERVICE);
alarmManager.setRepeating(alarmManager.RTC_WAKEUP,System.currentTimeMillis(),1*1000,pendingIntent);
}
}
这是我的日志
它每分钟都在重复,但我每秒钟都在写。我做错了什么吗 由于API 19,重复报警不准确。见此: 注:自API 19起,所有重复报警均不准确。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,并按上述方式重新安排每次的时间。targetSdkVersion早于API 19的传统应用程序将继续将其所有报警(包括重复报警)视为精确报警 您必须将
targetSdkVersion
设置为低于19的值,或者使用一次性精确报警,并每次重新设置
这是
使用java.util.Timer的简单解决方案
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//Put your code here
Log.d("Log", "Hello World");
}
}, 0, 1000);
但是说注意:Alarm Manager适用于您希望在特定时间运行应用程序代码的情况,即使您的应用程序当前未运行。
所以我使用了它,您可以显示一次性精确报警的示例吗?如果您只想让一个简单的计时器每秒执行一次操作,您也可以使用java定时器类。另一个解决方案是处理程序,这取决于您真正想要做的是什么,谢谢我将使用简单的处理程序,是否有机会通过alarm manager实现这一点?是的,但对于频繁重复的操作(如计数)来说,这可能不是那么有效和最好的解决方案。我可以在回答中给你举几个例子