android 应用程序需要哪些权限才能使用警报管理器服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11237920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:27:29  来源:igfitidea点击:

which permissions an android application need in order to use the Alarm Manager Service?

androidpermissionsalarm

提问by tomtu

If an android application wants to use the Alarm Manager Service, then which permissions the application needs to have?

如果一个android应用程序想要使用Alarm Manager Service,那么该应用程序需要具有哪些权限?

I have tested that it seems that application does not need to have any permission to use the Alarm Manager Service.

我已经测试过,应用程序似乎不需要任何权限即可使用警报管理器服务。

Is that true?

真的吗?

回答by Cristian

Yes, it is true. You do not have to add any special service. Keep in mind that when the handset is restarted the alarms you have set will be lost, so you may want to re-schedule them at boot time, which requires the android.permission.RECEIVE_BOOT_COMPLETEDpermission.

是的,它是真的。您不必添加任何特殊服务。请记住,当手机重新启动时,您设置的闹钟将丢失,因此您可能需要在启动时重新安排它们,这需要获得android.permission.RECEIVE_BOOT_COMPLETED许可。

回答by Mina Fawzy

I dont know why not any one mention this permission

我不知道为什么没有人提到这个权限

But according to android documentation , you should use SET_ALARM permission

但是根据android文档,你应该使用SET_ALARM权限

Documentation

文档

Allows an application to broadcast an Intent to set an alarm for the user.

允许应用程序广播 Intent 为用户设置警报。

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

回答by Ponmalar

It wakes CPU every 10 minutes until the phone turns off.

它每 10 分钟唤醒一次 CPU,直到手机关机。

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

<receiver  android:process=":remote" android:name="Alarm"></receiver>

If you want set alarm repeating at phone boot time:

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

<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>

For more details :Alarm Manager Example

有关更多详细信息:警报管理器示例

回答by kaushikSuman

Add to Manifest.xml:

添加到 Manifest.xml:

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

code:

代码:

  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;

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, "YOUR TAG");
             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);
     }
    }

If you want set alarm repeating at phone boot time:

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

Add permission to Manifest.xml:

向 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>

回答by Vipul Shah

Would like to add few bits to what CristianSaid

想对克里斯蒂安说的内容添加一些内容

Even if you use android.permission.RECEIVE_BOOT_COMPLETEDpermission your application will run properly on 2.X.X devices.

即使您使用android.permission.RECEIVE_BOOT_COMPLETED权限,您的应用程序也能在 2.XX 设备上正常运行。

But in 4.x devices the broadvast receiver will not work on Boot until and unless you start application manually

但是在 4.x 设备中,除非您手动启动应用程序,否则 Broadvast 接收器将无法在 Boot 上工作