gpt4 book ai didi

java - Android alarmManager,BroadcastReceiver onReceive 从未调用过

转载 作者:行者123 更新时间:2023-11-30 05:06:31 30 4
gpt4 key购买 nike

我正在尝试设置闹钟以在特定时间开始工作,但我的闹钟从未触发,我不明白我错过了什么。我正在使用 android api 22。

谢谢。

public class MainActivity extends ProtectedActivity {
@Override
public void onResume() {
super.onResume();
if (terminalSettings.GetSettlementEnabled())
{
startAlarmManager();
}
}

private void startAlarmManager()
{
Intent i = new Intent(MainActivity.this,
AlarmBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,
i, 0);

AlarmManager am =( AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pi); //
Millisec * Second * Minute

}

}

类 AlarmBroadcastReceiver

public class AlarmBroadcastReceiver extends BroadcastReceiver {


public AlarmBroadcastReceiver() {
super();
}

@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm !!!!!!!!!!",
Toast.LENGTH_LONG).show(); // For example

}
}

在 list 中我添加了那些行

<receiver android:name=".AlarmBroadcastReceiver"></receiver>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

最佳答案

你需要调用registerReceiver

public static final String Broadcast_PLAY_NEW_AUDIO = "Some key so you know which broadcast is which";

//Create Receiver
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Do stuff
}
};

//Register receiver
IntentFilter filter = new IntentFilter(HomeActivity.Broadcast_PLAY_NEW_AUDIO);
registerReceiver(receiver, filter);

//Send Broadcast
Intent broadcastIntent = new Intent(Broadcast_PLAY_NEW_AUDIO);
sendBroadcast(broadcastIntent);

关于java - Android alarmManager,BroadcastReceiver onReceive 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54482468/

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