gpt4 book ai didi

android - 向警报添加 Intent 但不立即运行

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

我的场景是我想在我用 AlarmManager 设置的特定时间运行接收器。

这里是一些相关的代码:

AlarmManager aManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(mContext, MyBroadcastReceiver.class);
aManager.set(AlarmManager.RTC, myTimeInMillis, PendingIntent.getBroadcast(context, 0, intent, 0));

我已经尝试将 FLAG_RECEIVER_REGISTERED_ONLY 标志添加到 Intent 中,这通过在我创建 Intent 时没有立即运行而达到了预期的效果,但 AlarmManager 似乎无法在设置该标志时运行接收器.

是否可以在 Android 中将运行广播接收器的 Intent 添加到 AlarmManager?

最佳答案

这是工作代码。它每 10 分钟唤醒一次 CPU,直到手机关机。

添加到 Manifest.xml:

...
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
...
<receiver android:process=":remote" android:name="Alarm"></receiver>
...

代码:

    package YourPackage;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.widget.Toast;

public class Alarm extends BroadcastReceiver
{
@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, "");
wl.acquire();

// Put here YOUR code.
Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

wl.release();
}

public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, Alarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
}

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

从服务设置警报:

package YourPackage;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class YourService extends Service
{
Alarm alarm = new Alarm();
public void onCreate()
{
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
alarm.SetAlarm(YourService.this);
return START_STICKY;
}



public void onStart(Context context,Intent intent, int startId)
{
alarm.SetAlarm(context);
}

@Override
public IBinder onBind(Intent intent)
{
return null;
}
}

如果你想在手机开机时设置闹钟重复:

为 Manifest.xml 添加权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
...
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
...

并创建新类:

package YourPackage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AutoStart extends BroadcastReceiver
{
Alarm alarm = new Alarm();
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
alarm.SetAlarm(context);
}
}
}

关于android - 向警报添加 Intent 但不立即运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29258278/

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