gpt4 book ai didi

android - AlarmManager 未在 Android 上启动

转载 作者:行者123 更新时间:2023-11-30 01:18:40 25 4
gpt4 key购买 nike

我想在 android 上设置闹钟,但它不起作用,这是我的代码..我使用挂起的 Intent 在特定时间启动警报,但是当我在我的设备上运行该应用程序时它没有启动。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);

findViewById(R.id.startAlarm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);


Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 39);

/* Repeating on every 20 minutes interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, pendingIntent);

Toast.makeText(MainActivity.this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
});

findViewById(R.id.stopAlarm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
Toast.makeText(MainActivity.this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
});
}

我是android的新手,所以请帮忙。这是我的接收器类

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// For our recurring task, we'll just display a message
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}

最佳答案

您是否在 list 中声明了您的接收者?

      <receiver android:name="myPackage.AlarmReceiver"
android:enabled="true" >
</receiver>

关于android - AlarmManager 未在 Android 上启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37435651/

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