gpt4 book ai didi

安卓 AlarmManager 不工作

转载 作者:行者123 更新时间:2023-11-29 15:56:37 26 4
gpt4 key购买 nike

我在论坛上搜索过,但它对其他人有用。我的 AlarmManager 有什么问题?我想每分钟调用一次CallDataSend类

public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Intent alarmIntent = new Intent(this, CallDataSend.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
alarmIntent, PendingIntent.FLAG_ONE_SHOT);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
}
}

CallDataSend 类:

public final class CallDataSend extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

}
}

和xml文件:

<receiver
android:name="CallDataSend"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</receiver>

最佳答案

我认为您应该先启用接收器,然后系统才能对其进行实例化。

<receiver
android:name="CallDataSend"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</receiver>

关于安卓 AlarmManager 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27636580/

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