1. 程式人生 > >AlarmManager定時重複任務,傳送心跳

AlarmManager定時重複任務,傳送心跳

package com.example.alarm_1;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Intent intent = new Intent();
		intent.setClass(MainActivity.this, MyService.class);

		PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent,
				0);

		AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
		Log.i("Alarm", "Alarm");
		// am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5*1000,
		// pendingIntent);  ///單次不重複

		 am.setRepeating(AlarmManager.RTC_WAKEUP ,
		 System.currentTimeMillis(), 10*1000, pendingIntent);//////TTTT   重複
//		am.setInexactRepeating(AlarmManager.RTC_WAKEUP,
//				System.currentTimeMillis(), 10 * 1000, pendingIntent);////TTTT  重複

	}
}
package com.example.alarm_1;

import java.util.Date;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/**
 * @author Administrator
 * 
 */
public class MyService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		// TODO 自動生成的方法存根
		return null;
	}

	@Override
	public void onCreate() {
		// TODO 自動生成的方法存根

	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO 自動生成的方法存根

		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO 自動生成的 catch 塊
			e.printStackTrace();
		}
		// player.stop();
		Log.i("--------------------暫停----------------------", "暫停");
		Toast.makeText(this, "暫停", Toast.LENGTH_SHORT).show();

	}

	@Override
	public void onDestroy() {
		// TODO 自動生成的方法存根

	}

}// / class MyService
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

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

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <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>
                <service
            android:name=".MyService"
            android:enabled="true" >
        </service>
    </application>

</manifest>

效果是:每隔10s執行一次服務,延遲5s後顯示“暫停”提示,迴圈發生。這個有大作用,這5s可以訪問資料庫,往裡寫資料,或者保持線上狀態,