handler在Activity中定义成public static形式,Service使用sendMessage()向Activity发送消息,Activity使用handleMessage()修改UI
package com.example.alarm_1;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
UserId userId = new UserId();
private TextView textView1;
public static Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
UserId.setId1("123");
UserId.setId2("456");
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(),
5 * 1000, pendingIntent);// ////TTTT 重复
// /////////
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO 自动生成的方法存根
// super.handleMessage(msg);
if (msg.what == 1) {
String text = (String) msg.obj;
textView1.setText(text);
}
}
};// handler
}// /onCreate
}// /MainActivity
package com.example.alarm_1;
import java.util.Date;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
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) {
String id11 = UserId.getId1();
String id22 = UserId.getId2();
Log.i("---------------onStart--------------",
"-----------onStart---------------");
Log.i("--------------------暂停----------------------", "暂停");
Toast.makeText(this, "暂停" + id11 + "," + id22, Toast.LENGTH_SHORT)
.show();
Message msg = new Message();
msg.what=1;
//msg.obj="暂停";
msg.obj=String.valueOf(System.currentTimeMillis());
MainActivity.handler.sendMessage(msg);
}
@Override
public void onDestroy() {
// TODO 自动生成的方法存根
Log.i("---------------onDestroy--------------",
"-----------onDestroy---------------");
}
}//
package com.example.alarm_1;
public class UserId {
private static String id1;
private static String id2;
public static String getId1() {
return id1;
}
public static void setId1(String id1) {
UserId.id1 = id1;
}
public static String getId2() {
return id2;
}
public static void setId2(String id2) {
UserId.id2 = id2;
}
}
<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:id="@+id/textView1"
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>
时间每5s变化一次,Toast每5s弹出一次