问题描述
我在我的应用中使用带有闹钟管理器的唤醒广播接收器来在特定时间执行一些任务。 它运作良好。但我面临一个小问题 例如,我在晚上 9:00 设置我的闹钟管理器,如果我从最近的应用程序中删除我的应用程序,它肯定会在后台运行,但是在特定时间(晚上 9:00)之后,每当我使用我的应用程序时,我的闹钟都会立即触发. 我希望每个人(有关这个问题)都能理解我的观点。 如果有人知道这个问题,请告诉我。 谢谢。
<receiver
android:name="com.example.chartdemo.MybroadcastReceiver"
android:enabled="true"
android:exported="true">
</receiver>
这是我的 MainActivity.java 代码
public class MainActivity extends AppCompatActivity {
public static TextView textView,textView1;
Button button,button1,button2,button3;
public static int counter=0,time;
MybroadcastReceiver mybroadcastReceiver;
myDbAdapter helper;
//private final MyDateChangeReceiver mDateReceiver = new MyDateChangeReceiver();
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.text);
textView1=findViewById(R.id.text1);
button=findViewById(R.id.counterbtn);
button1=findViewById(R.id.showbtn);
button2=findViewById(R.id.savebtn);
button3=findViewById(R.id.delbtn);
helper = new myDbAdapter(this);
loadData(); //for data retrieving from shared preferences
startAlarm(this);
}
private void startAlarm(Context context) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,9);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);
cal.set(AM_PM,PM);
time= (int) cal.getTimeInMillis();
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
Intent intent = new Intent(context,MybroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getbroadcast(context,234,intent,0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),pendingIntent);
Toast.makeText(context,"alarm set after "+cal.getTimeInMillis(),Toast.LENGTH_SHORT).show();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP,Toast.LENGTH_SHORT).show();
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP,Toast.LENGTH_SHORT).show();
}
}
这是我的 WakefulbroadcastReceiver.java 代码
public class MybroadcastReceiver extends WakefulbroadcastReceiver {
MediaPlayer mp;
myDbAdapter helper;
MainActivity mainActivity=new MainActivity();
@Override
public void onReceive(Context context,Intent intent) {
SharedPreferences sh = context.getSharedPreferences("MySharedPref",MODE_PRIVATE);
int a = sh.getInt("counter",0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
helper = new myDbAdapter(context);
long id = helper.insertData(date,String.valueOf(a));
if (id <= 0) {
Toast.makeText(context,"Insertion UnSuccessful",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context,"Insertion Successful",Toast.LENGTH_SHORT).show();
}
mp = MediaPlayer.create(context,R.raw.alarm);
mp.start();
Toast.makeText(context,"Alarm...!!!!!! ",Toast.LENGTH_LONG).show();
SharedPreferences sharedPreferences = context.getSharedPreferences("MySharedPref",MODE_PRIVATE);
SharedPreferences.Editor myEdit = sharedPreferences.edit();
myEdit.putInt("counter",0);
myEdit.commit();
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)