gpt4 book ai didi

android - 警报管理器和广播接收器不停止

转载 作者:太空狗 更新时间:2023-10-29 13:24:46 25 4
gpt4 key购买 nike

我想在多个特定日期和时间创建通知,这些通知存储在数据库中。我可以在正确的日期和时间收到通知,但后来我注意到,我也在第二天随机收到通知。每当我重新启动模拟器时,它们都会不断出现。

所以,我似乎无法停止警报管理器或广播接收器。我试过向 alarmmanager 的取消方法提供 pendingIntent 但无济于事。我也使用切换按钮来启用/禁用通知,但它没有效果

下面是我的代码。

主 Activity .java

PendingIntent pendingIntent;
AlarmManager alarmManager;
Button set, cancel;
MTPAdapter db;
ArrayList<TourPlan> arrDates;

ToggleButton toggle;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
toggle = (ToggleButton) findViewById(R.id.toggleButton1);

// This function fetched data from DB
fetchDates();



set = (Button) findViewById(R.id.the_button);
cancel = (Button) findViewById(R.id.the_button_cancel);
cancel.setEnabled(false);



toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
Toast.makeText(getApplicationContext(), "Set",
Toast.LENGTH_SHORT).show();
setEvent();
} else {
alarmManager.cancel(pendingIntent);
MyReceiver.stopService(getApplicationContext());
}
}
});
} // end onCreate

public void setEvent() {

for (int i = 0; i < arrDates.size(); i++) {
// int id = arrDates.get().getId();

int year = Integer.parseInt(arrDates.get(i).getYear());

int month = Integer.parseInt(arrDates.get(i).getMonth());

int date = Integer.parseInt(arrDates.get(i).getDate());

int hour = Integer
.parseInt(firstStringer(arrDates.get(i).getTime()));
Log.v("Hour : ", "" + hour);

int minute = Integer.parseInt(stringer(arrDates.get(i).getTime()));
Log.v("minute : ", "" + minute);
int second = 00;
startAlarm(i, year, month, date, hour, minute, second);
}
}

public void startAlarm(int id, int year, int month, int date, int hour,
int minute, int second) {
Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.DAY_OF_MONTH, date);

calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
// calendar.set(Calendar.AM_PM, Calendar.PM);

Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);

pendingIntent = PendingIntent.getBroadcast(MainActivity.this, id,
myIntent, 0);

alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(),
pendingIntent);
}


}

MyAlarmService.java

public class MyAlarmService extends Service {


@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);



NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("ISFA")
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText("Tour Planned for the Day"))
.setContentText("You have planned a tour for today")
.setAutoCancel(true);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}

MyReceiver.java(广播接收器):

  public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);

}

public static void stopService(Context context) {
Intent stopServiceIntent = new Intent(context, MyAlarmService.class);
context.stopService(stopServiceIntent);
}
}

list 文件:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

在应用标签中,

 <service
android:name="com.example.brodcastalarm.MyAlarmService"
android:enabled="true" />
<!-- THE BROADCAST RECIEVER WHICH MANAGES THE BROADCAST FOR NOTIFICATION -->
<receiver android:name="com.example.brodcastalarm.MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

这是数据库结构。我使用 subString 方法获取小时和分钟。

This is Database Sample

最佳答案

警报管理器需要手动禁用,为此使用下面的代码,它将禁用警报。

AlarmManager aManager = (AlarmManager) getSystemService(ALARM_SERVICE);         
Intent intent = new Intent(getBaseContext(), YourScheduleClass.class);
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
aManager.cancel(pIntent);

关于android - 警报管理器和广播接收器不停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22705776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com