无法创建报警管理器 [英] Can not create Alarm Manager

查看:174
本文介绍了无法创建报警管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的英语不好。我面临的问题。我尝试创建持续的报警通知。警报通知必须开始每10秒。我使用报警管理器,但它doest't工作。我在做什么错了?

 公共类RemindReceiver扩展广播接收器{私有类<> activityClass;公共RemindReceiver(){}公共RemindReceiver(类<> activityClass){    this.activityClass = activityClass;
}@覆盖
公共无效的onReceive(上下文的背景下,意图意图){    NotificationManager notifyManager =(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);    通知通知=新的通知(R.mipmap.ic_launcher,一些文本,System.currentTimeMillis的());    意图int​​entTL =新意图(背景下,activityClass);
    notification.setLatestEventInfo(背景下,标题,一些文本
            PendingIntent.getActivity(上下文,0,intentTL,PendingIntent.FLAG_CANCEL_CURRENT));
    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
    notifyManager.notify(1,通知);    AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);    的PendingIntent的PendingIntent = PendingIntent.getBroadcast(背景下,0,意向,PendingIntent.FLAG_CANCEL_CURRENT);    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,System.currentTimeMillis的(),1000 * 5的PendingIntent);
}公共无效setRemind(上下文的背景下){    AlarmManager alarmManager =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    意向意图=新意图(背景下,RemindReceiver.class);
    的PendingIntent的PendingIntent = PendingIntent.getBroadcast(上下文,0,意图,0);
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,System.currentTimeMillis的(),1000 * 5的PendingIntent);
}}

片段:

 公共类PersonListFragment扩展片段{私人RemindReceiver remindReceiver;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){    查看rootView = inflater.inflate(R.layout.person_list_fragment_layout,集装箱,FALSE);    按钮Next按钮=(按钮)rootView.findViewById(R.id.next_button);
    ListView控件personListView =(ListView控件)rootView.findViewById(R.id.name_list_view);
    清单<&人GT; personList = PersonListGenerator.generate();    PersonListAdapter适配器=新PersonListAdapter(getActivity(),personList);
    personListView.setAdapter(适配器);    上下文的背景下= getActivity()getApplicationContext()。
    remindReceiver =新RemindReceiver(PersonListActivity.class);
    remindReceiver.setRemind(上下文);
    remindReceiver.onReceive(getActivity(),新的意向书());    nextButton.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){            意向意图=新意图(getActivity(),ExpandablePersonListActivity.class);
            startActivity(意向);
        }
    });    返回rootView;
}}

和我一块Android清单的:

 <使用许可权的android:NAME =android.permission.RECEIVE_BOOT_COMPLETED/>
<使用许可权的android:NAME =android.permission.WAKE_LOCK/>
<接收机器人:名字=。utility.RemindReceiver
        机器人:启用=假>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.BOOT_COMPLETED>< /作用>
        &所述; /意图滤光器>
    < /接收器>


解决方案

你能尝试SystemClock.elapsedRealtime()?

例如:

  alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime(),1000 * 5的PendingIntent);

我依稀记得AlarmManager.ELAPSED_REALTIME_WAKEUP需要SystemClock.elapsedRealtime(),而不是System.currentTimeMillis的()

我却无法找到在哪里我之前阅读。当我找到它,我会更新。

编辑:

据:<一href=\"http://developer.android.com/reference/android/app/AlarmManager.html#set(int,%20long,%20android.app.PendingIntent)\"相对=nofollow> AlarmManager.set(int型的,长triggerAtMillis,操作的PendingIntent)SDK文档..


  

参数


  
  

      
  • 类型:一ELAPSED_REALTIME,ELAPSED_REALTIME_WAKEUP,RTC,或RTC_WAKEUP的。以毫秒为单位triggerAtMillis时间报警应
      熄灭,使用

  •   
  • 酌情:时钟(取决于报警类型)。

  •   
  • 操作:操作时警报响起时执行;通常来自IntentSender.getBroadcast()。

  •   

Sorry for my bad English. I faced with the problem. I try to create persistent alarm notification. Alarm notification must start every 10 seconds. I am using Alarm Manager,but it doest't work. What i am doing wrong?

public class RemindReceiver extends BroadcastReceiver {

private Class<?> activityClass;

public RemindReceiver() {

}

public RemindReceiver(Class<?> activityClass) {

    this.activityClass = activityClass;
}

@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.mipmap.ic_launcher, "Some Text", System.currentTimeMillis());

    Intent intentTL = new Intent(context, activityClass);
    notification.setLatestEventInfo(context, "Title", "Some Text",
            PendingIntent.getActivity(context, 0, intentTL, PendingIntent.FLAG_CANCEL_CURRENT));
    notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
    notifyManager.notify(1, notification);

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

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000 * 5, pendingIntent);
}

public void setRemind(Context context) {

    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, RemindReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000 * 5 , pendingIntent);
}}

Fragment:

public class PersonListFragment extends Fragment {

private RemindReceiver remindReceiver;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.person_list_fragment_layout, container, false);

    Button nextButton = (Button) rootView.findViewById(R.id.next_button);
    ListView personListView = (ListView) rootView.findViewById(R.id.name_list_view);
    List<Person> personList = PersonListGenerator.generate();

    PersonListAdapter adapter = new PersonListAdapter(getActivity(), personList);
    personListView.setAdapter(adapter);

    Context context = getActivity().getApplicationContext();
    remindReceiver = new RemindReceiver(PersonListActivity.class);
    remindReceiver.setRemind(context);
    remindReceiver.onReceive(getActivity(), new Intent());

    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(getActivity(), ExpandablePersonListActivity.class);
            startActivity(intent);
        }
    });

    return rootView;
}}

And piece of my Android Manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<receiver android:name=".utility.RemindReceiver"
        android:enabled="false">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>

解决方案

Can you try SystemClock.elapsedRealtime() ?

e.g.:

    alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1000 * 5, pendingIntent);

I vaguely remember AlarmManager.ELAPSED_REALTIME_WAKEUP needing SystemClock.elapsedRealtime() and not System.currentTimeMillis()

I can't however find where I read this before. I will update when I find it.

edit:

According to: AlarmManager.set(int type, long triggerAtMillis, PendingIntent operation) sdk docs..

Parameters

  • type: One of ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC, or RTC_WAKEUP. triggerAtMillis time in milliseconds that the alarm should go off, using the
  • appropriate: clock (depending on the alarm type).
  • operation: Action to perform when the alarm goes off; typically comes from IntentSender.getBroadcast().

这篇关于无法创建报警管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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