能实现定时器的方法有好几种,而利用AlarmManager发送广播的方式实现相对来说还是很靠谱的,长期在后台运行会被系统列入高耗电APP,系统会自动停掉广播的发送,除非把APP加入系统白名单,上代码:
package com.example.myapplication;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.databinding.FragmentFirstBinding;
import static android.content.Context.ALARM_SERVICE;
public class FirstFragment extends Fragment {
private FragmentFirstBinding binding;
/**
* 10秒
*/
private static final int TIME_INTERVAL = 10000;
private PendingIntent pendingIntent;
private AlarmManager alarmManager;
private static final String ALARM_ACTION = "com.example.myapplication.alarm.clock";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = FragmentFirstBinding.inflate(inflater, container, false);
return binding.getRoot();
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter intentFilter = new IntentFilter(ALARM_ACTION);
getActivity().registerReceiver(alarmReceiver, intentFilter);
alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, new Intent(ALARM_ACTION), 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//6.0低电耗模式需要使用此方法才能准时触发定时任务
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//4.4以上,使用此方法触发定时任务时间更为精准
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), pendingIntent);
} else {//4.4以下,使用旧方法
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), TIME_INTERVAL, pendingIntent);
}
}
@Override
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(alarmReceiver);
alarmManager.cancel(pendingIntent);
}
private BroadcastReceiver alarmReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//定时任务
Log.d("Alex", "onReceive");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + TIME_INTERVAL, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + TIME_INTERVAL, pendingIntent);
} else {//4.4以下,使用旧方法
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), TIME_INTERVAL, pendingIntent);
}
}
};
}
这是一个实现的例子,首次发送闹钟广播不加延迟时间,第2次开始延迟一定的时间,这里是10秒,时间可以自己定义。
这里广播接收器用的是动态注册,根据官方文档可知,安卓8.0开始,一般情况静态注册广播接收器无效。
广播概览 ?|? Android 开发者 ?|? Android Developers
?再注意一些注意点:
根据性能和需求有3种发送广播的方式:
安全事项以及最佳做法:
?总结一下就是:
应用内尽量使用?LocalBroadcastManager,因为广播是跨进程的,而本地广播只在应用内有效,性能会好很多;onReceive()?运行在主线程中,避免使用耗时的操作。
好了,又可以愉快的玩耍了。
|