AlarmManager不工作 [英] AlarmManager isn't working

查看:177
本文介绍了AlarmManager不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多这样的问题,但没有解决方案的帮助了我。我尝试安排使用AlarmManager一个BroadcastReciever。现在,所有应该做的是做一个面包和写东西到一个文件中。
这里是我的code,我既得不到敬酒报警,也不是创建日志文件。
在MainActivity的吐司所示。任何想法?

下面是清单,

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.android.alarmtest>
    <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <使用许可权的android:NAME =com.android.alarm.permission.SET_ALARM/>
    <应用
        机器人:allowBackup =真
        机器人:图标=@的mipmap / ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:supportsRtl =真
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ APP_NAME
            机器人:主题=@风格/ AppTheme.NoActionBar>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <接收机器人:名字=。AlarmReciever/>
        <服务机器人:启用=真正的机器人:名字= />中为MyService。
    < /用途>< /清单>

MainActivity

 公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        很长一段时间=新的GregorianCalendar(2015,12,4,9,44).getTimeInMillis();        意图int​​entAlarm =新意图(这一点,AlarmReciever.class);
        intentAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        AlarmManager alarmManager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);        alarmManager.set(AlarmManager.RTC_WAKEUP,时间,PendingIntent.getBroadcast(这一点,1,intentAlarm,PendingIntent.FLAG_UPDATE_CURRENT));
        Toast.makeText(这一点,报警计划,Toast.LENGTH_LONG).show();
    }}

和Reciever

 公共类AlarmReciever扩展广播接收器{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        Toast.makeText(背景下,报警,Toast.LENGTH_LONG);
        日志(报警);
    }    公共无效日志(String s)将{
        Log.e(国家,S);
        文件f =新的文件(Environment.getExternalStorageDirectory()的log.txt);
        尝试{
            如果f.createNewFile()(f.exists()!);
            FileWriter的FileWriter的=新的FileWriter(F,真);
            fileWriter.write(S +\\ n);
            fileWriter.close();
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }
}


解决方案

能否请你试试这个?

  INT trigger_time = System.currentTimeMillis的()+ 30 * 1000;如果(Build.VERSION.SDK_INT> = 19){
     alarmManager.setExact(AlarmManager.RTC_WAKEUP,trigger_time,的PendingIntent);
 }其他{
     alarmManager.set(AlarmManager.RTC_WAKEUP,trigger_time,的PendingIntent);
 }

编辑:

区别之间的 setRepeating setInexactRepeating

确定你的闹钟怎么precise必须

选择报警类型往往是在创建警报的第一步。另一个区别是你如何precise需要你的闹钟是。

对于大多数应用程序, setInexactRepeating()是正确的选择。当您使用此方法, Android的同步多个不精确的重复报警和同时激发他们。这样可以减少在流失电池

有关罕见的应用程序,都有严格的时间要求,例如,警报需要解雇的 precisely 下午12:00 日常然后用 setRepeating()

希望这会帮助你。

I know there are a lot of questions like this, but none of the solutions helped me. I try to schedule a BroadcastReciever using the AlarmManager. For now, all it should do is make a Toast and write something to a file. Here is my code, i get neither the toast "alarm" nor is the logfile created. The Toast of the Mainactivity is shown. Any Ideas?

Here is the Manifest,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.alarmtest" >
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".AlarmReciever"/>
        <service android:enabled="true" android:name=".MyService" />
    </application>

</manifest>

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        long time = new GregorianCalendar(2015,12,4,9,44).getTimeInMillis();

        Intent intentAlarm = new Intent(this, AlarmReciever.class);
        intentAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,  intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
        Toast.makeText(this, "Alarm Scheduled", Toast.LENGTH_LONG).show();
    }

}

and the Reciever

public class AlarmReciever extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"alarm",Toast.LENGTH_LONG);
        log("alarm");
    }

    public void log(String s) {
        Log.e("STATE", s);
        File f = new File(Environment.getExternalStorageDirectory(),"log.txt");
        try {
            if (!f.exists()) f.createNewFile();
            FileWriter fileWriter = new FileWriter(f, true);
            fileWriter.write(s+"\n");
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

解决方案

Can you please try this one ?

int trigger_time= System.currentTimeMillis() + 30 * 1000;   

if(Build.VERSION.SDK_INT >= 19) {
     alarmManager.setExact(AlarmManager.RTC_WAKEUP, trigger_time, pendingIntent);
 } else {
     alarmManager.set(AlarmManager.RTC_WAKEUP, trigger_time, pendingIntent);
 }

Edit:

Difference between setRepeating and setInexactRepeating:

Decide how precise your alarm needs to be

Choosing the alarm type is often the first step in creating an alarm. A further distinction is how precise you need your alarm to be.

For most apps, setInexactRepeating() is the right choice. When you use this method, Android synchronizes multiple inexact repeating alarms and fires them at the same time. This reduces the drain on the battery.

For the rare app that has rigid time requirements as example, the alarm needs to fire precisely at 12:00 p.m. everyday then use setRepeating().

Hope this will help you.

这篇关于AlarmManager不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆