每2分钟调度一次android [英] Scheduling alarm every 2 minutes android

查看:154
本文介绍了每2分钟调度一次android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动班上

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,System.currentTimeMillis(),2000, pendingIntent);

    }

还有我的onrecieve函数在alarmreciever类中

And My onrecieve function in alarmreciever class

     @Override
     public void onReceive(Context context, Intent intent)
      {   
        //get and send location information
         System.out.println("fired");
      }

我使用的是nexus 4,kitkat版本.我没有看到每2分钟触发任何onreceive函数.nthg正在发生... 有什么帮助吗?谢谢

I am using nexus 4, kitkat version. I don't see any onreceive function fired every 2 minutes.nthg is happening... any help? thank you

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name="com.example.AlarmExample"
        android:exported="false" >
    </receiver>
</application>
 </manifest>

我也放了清单. ...................................................

I just put my manifest as well. ................................................

推荐答案

在setRepeating函数中,应将SystemClock.elapsedRealTime()用于ELAPSED_REALTIME_WAKEUP. 另外,您需要将2000更改为2 * 60 * 1000以指定间隔时间.

In your setRepeating function, you should use SystemClock.elapsedRealTime() for ELAPSED_REALTIME_WAKEUP. Also, you need to change 2000 to 2*60*1000 to specify your interval time.

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

希望这会有所帮助.

参考: ELAPSED_REALTIME_WAKEUP

在清单文件中,您的接收者名称中有一个错字. 将".AlarmReciever"更改为".AlarmReceiver".

In your manifest file, there is a typo in your receiver name. Change ".AlarmReciever" to ".AlarmReceiver".

<receiver
    android:name=".AlarmReceiver"
    android:exported="true" >
</receiver>

这篇关于每2分钟调度一次android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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