简体   繁体   English

从片段内部调用时,警报管理器BroadcastReceiver,setRepeating不起作用

[英]Alarm Manager BroadcastReceiver, setRepeating isn't working when called from inside a Fragment

Hello everybody this is my first post here and I must say I really like this site it helped me fix a lot of things. 大家好,这是我在这里的第一篇文章,我必须说我真的很喜欢这个网站,它帮助我解决了很多问题。 But now I have this problem when useing Alarm Manager in android from inside a Fragment. 但是现在从片段内部在Android中使用Alarm Manager时出现了这个问题。

Here is my code (snippet from stackoverflow), it used to work I tried to set the minutes to random and then it suddenly stopped working. 这是我的代码(stackoverflow的摘录),它曾经可以正常工作,我试图将分钟数设置为随机,然后突然停止工作。 When I removed it the error was still there.. 当我删除它时,错误仍然存​​在。

Alarm.java : Alarm.java:

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);
 }
}

RealityCheck.java RealityCheck.java

import com.actionbarsherlock.app.SherlockFragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.CheckBox;
import android.widget.Toast;

public class RealityCheck extends SherlockFragment implements View.OnClickListener {

    CheckBox EnabledBox;
    Alarm alarm = new Alarm();
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        //Moet inflater gebruiken om findViewById in een Fragment te gebruiken en op het einde returnen
        View rootView = inflater.inflate(R.layout.activity_reality_check, container, false);
        EnabledBox = (CheckBox)rootView.findViewById(R.id.check_box_enabled);
        EnabledBox.setOnClickListener(this);
        return rootView;

    }
    @Override
    public void onClick(View v) {
        CheckBox EnabledBox = (CheckBox) v;
        if(EnabledBox.isChecked())
        {
            //Moet this.getActivity gebruiken in Fragment
            Toast.makeText(RealityCheck.this.getActivity(), "Reality check alarm activated", Toast.LENGTH_SHORT).show();
            //Alarm starten (in fragment weer getActivity() ipv Context)
            alarm.SetAlarm(getActivity());
            }
        else
        {
            Toast.makeText(RealityCheck.this.getActivity(), "Reality check alarm disabled", Toast.LENGTH_SHORT).show();
            alarm.CancelAlarm(getActivity());
        }   

    }   
}

manifest: 表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dreamincubation"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock" >

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

        <activity
            android:name="com.example.dreamincubation.IntroWindow"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.dreamincubation.MainActivity"
            android:label="@string/title_activity_main_menu" >
        </activity>
        <activity
            android:name="com.example.dreamincubation.RealityCheck"
            android:label="@string/title_activity_reality_check" >
        </activity>
        <activity
            android:name="com.example.dreamincubation.IncubationInfo"
            android:label="@string/title_activity_incubation_info" >
        </activity>
        <activity
            android:name="com.example.dreamincubation.Incubation"
            android:label="@string/title_activity_incubation" >
        </activity>
        <activity
            android:name="com.example.dreamincubation.AlarmService"
            android:label="@string/title_activity_alarm_service" >
        </activity>
         <receiver android:name="Alarm" >
        </receiver>
    </application>
</manifest>

I've tried it without the wake-lock. 我已经尝试了没有唤醒锁。 Didn't work either. 也没用。

If anybody can help me out it would be greatly appreciated.. 如果有人可以帮助我,将不胜感激。

You are telling to the AlarmManager to trigger immediatly by doing this : 您正在通过以下步骤告诉AlarmManager立即触发:

 am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); 

And it should repeat the alarm every 10 minutes. 并且应该每10分钟重复一次警报。

but if you want the first event to be in 10min and to repeat every 10 min you have to do this : 但是,如果您希望第一个事件发生在10分钟内并每10分钟重复一次,则必须这样做:

 long tenMin = 1000 * 60 * 10;
 am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + tenMin, tenMin, pi); 

I recommend to deal with the AlarmManager in a Service, a great tutorial explain it : 我建议在Service中处理AlarmManager,一个很棒的教程对此进行了解释:

http://blog.blundell-apps.com/notification-for-a-user-chosen-time http://blog.blundell-apps.com/notification-for-a-user-chosen-time

If you don't need an exact time alarm, you should definitly use the method : 如果您不需要确切的时间警报,则应明确使用方法:

setInexactRepeating

http://developer.android.com/reference/android/app/AlarmManager.html#setInexactRepeating(int , long, long, android.app.PendingIntent) http://developer.android.com/reference/android/app/AlarmManager.html#setInexactRepeating(int,long,long,android.app.PendingIntent

I order to preserve battery. 我要保留电池。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM