大家好,这是我在这里发表的第一篇文章,我必须说我真的很喜欢这个网站,它帮我解决了很多问题。但是现在我在片段内部在android中使用Alarm Manager时遇到了这个问题。
这是我的代码(来自stackoverflow的代码段),它曾经工作我试图将分钟设置为随机,然后它突然停止工作。当我删除它时,错误仍然存在..
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
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());
}
}
}
清单:
<?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>
我没有唤醒锁就试过了。也没用。
如果有人可以帮助我,我们将不胜感激。
答案 0 :(得分:0)
您告诉AlarmManager立即触发:
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi);
它应该每10分钟重复一次警报。
但如果您希望第一个事件在10分钟内并且每10分钟重复一次,您必须这样做:
long tenMin = 1000 * 60 * 10;
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + tenMin, tenMin, pi);
我建议在服务中处理AlarmManager,一个很好的教程解释它:
http://blog.blundell-apps.com/notification-for-a-user-chosen-time
如果您不需要确切的时间警报,您应该明确使用该方法:
setInexactRepeating
http://developer.android.com/reference/android/app/AlarmManager.html#setInexactRepeating(int,long,long,android.app.PendingIntent)
我是为了保护电池。