AlarmManager在android

时间:2018-05-03 09:07:27

标签: android android-asynctask background alarmmanager

实际上我想在特定时间之后运行后台线程,比如在10秒之后将Api发送到服务器但是我首先尝试下面的代码进行检查/测试。在AlarmReceiver类中,onReceive只运行一次,而不是每隔10秒就要运行一次。我是新来的android请指导我如何实现我的目标。谢谢

这是MainActivity Code:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Retrieve a PendingIntent that will perform a broadcast
        Intent alarmIntent = new Intent(this, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
    }


    public void startAlarm(View view) {
        manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        int interval = 10000; // 10 seconds

        manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
        Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
    }

    public void cancelAlarm(View view) {
        if (manager != null) {
            manager.cancel(pendingIntent);
            Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
        }

    }
}

这是AlarmReceiver类:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // For our recurring task, we'll just display a message
        Toast.makeText(arg0, "I'm running", Toast.LENGTH_SHORT).show();

    }

}

这是清单文件代码:

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver android:name=".AlarmReceiver"></receiver>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

我认为对于像秒一样的小时差,您可以使用 Handler

试试这段代码,

Handler handler=new Handler();
Runnable mytask=new Runnable(){
    public void run(){
        //do something
         Toast.makeText(Youractivity.this, "Running", Toast.LENGTH_SHORT).show();
        handler.postDelayed(this, 10000);//10 sec
    }
};
mytask.run();

修改

做一些技巧,

public class AlarmReceiver extends BroadcastReceiver
{


    @Override
    public void onReceive(Context context, Intent intent)
    {
        //yourtask
        setAlarm(context,10000);

    }
    public void setAlarm(Context context,long time)
    {
        AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + time, pi);
    }

    public void cancelAlarm(Context context)
    {
        try {
            Intent intent = new Intent(context, AlarmReceiver.class);
            PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(sender);
        }
        catch (Exception e)
        {

        }
    }
}

在你的活动中,

new AlarmReceiver().setAlarm(this,10000);