gpt4 book ai didi

android - 在没有服务的情况下使用 AlarmManager

转载 作者:行者123 更新时间:2023-11-29 01:54:16 25 4
gpt4 key购买 nike

是否可以在应用程序不使用服务的情况下使用 AlarmManager 运行警报。 AlarmManager 必须让设备退出 sleep 模式并在 onReceive 中执行我的代码。实际上,我在这里看到的每个代码示例都显示了正在使用的服务。然而,Android 文档并未提及需要提供服务。

最佳答案

这是一个工作示例,

activity_alarm_manager.xml文件的代码

<linearlayout android:layout_height="match_parent"
android:layout_width="match_parent" android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<button android:id="@+id/btStart" android:layout_height="wrap_content"
android:layout_width="match_parent" android:onclick="startRepeatingTimer"
android:padding="@dimen/padding_medium" android:text="@string/btStart"
tools:context=".WidgetAlarmManagerActivity"/>
<button android:id="@+id/btCancel" android:layout_height="wrap_content"
android:layout_width="match_parent" android:onclick="cancelRepeatingTimer"
android:padding="@dimen/padding_medium" android:text="@string/btCancel"
tools:context=".WidgetAlarmManagerActivity"/>
<button android:id="@+id/btOneTime" android:layout_height="wrap_content"
android:layout_width="match_parent" android:onclick="onetimeTimer"
android:padding="@dimen/padding_medium" android:text="@string/btOneTime"
tools:context=".WidgetAlarmManagerActivity"/>
</linearlayout>

BroadcastReceiver的代码,

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

final public static String ONE_TIME = "onetime";

@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
//Acquire the lock
wl.acquire();

//You can do the processing here.
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();

if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){
//Make sure this intent has been sent by the one-time timer button.
msgStr.append("One time Timer : ");
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
msgStr.append(formatter.format(new Date()));

Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();

//Release the lock
wl.release();
}

public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//After after 5 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pi);
}

public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}

public void setOnetimeTimer(Context context){
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
}

AndroidManifest.xml的代码

<manifest android:versioncode="1" android:versionname="1.0"
package="com.rakesh.alarmmanagerexample"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-sdk android:minsdkversion="10" android:targetsdkversion="15"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:label="@string/title_activity_alarm_manager"
android:name="com.rakesh.alarmmanagerexample.AlarmManagerActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.rakesh.alarmmanagerexample.AlarmManagerBroadcastReceiver">
</receiver>
</application>
</manifest>

AlarmManagerActivity.java文件的代码

public class AlarmManagerActivity extends Activity {

private AlarmManagerBroadcastReceiver alarm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_manager);
alarm = new AlarmManagerBroadcastReceiver();
}

@Override
protected void onStart() {
super.onStart();
}

public void startRepeatingTimer(View view) {
Context context = this.getApplicationContext();
if(alarm != null){
alarm.SetAlarm(context);
}else{
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}

public void cancelRepeatingTimer(View view){
Context context = this.getApplicationContext();
if(alarm != null){
alarm.CancelAlarm(context);
}else{
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}

public void onetimeTimer(View view){
Context context = this.getApplicationContext();
if(alarm != null){
alarm.setOnetimeTimer(context);
}else{
Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_widget_alarm_manager, menu);
return true;
}
}

来自 Github 的引用链接

关于android - 在没有服务的情况下使用 AlarmManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16117710/

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