Notepad学习笔记三
NoteEdit.java
package com.example.notepad;import java.text.SimpleDateFormat;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;import com.example.notepad.Notepad;public class MyService extends Service {private boolean flag = true;public static MyService service = null;private int i = 0;private Thread thread = null;@Overridepublic IBinder onBind(Intent arg0) {return null;}@Overridepublic void onCreate() {super.onCreate();service = this;thread = new Thread(new MyThread());}class MyThread implements Runnable {@Overridepublic void run() {// TODO Auto-generated method stubwhile (flag) {try {Thread.sleep(8000);} catch (InterruptedException e) {e.printStackTrace();}String s = "第" + (i++) + "次发送广播"+"\n";Log.i("send", s);Intent intent = new Intent(Notepad.INTENAL_ACTION_1);SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String date = "当前系统时间:"+sDateFormat.format(new java.util.Date()); intent.putExtra("message", s+date+"\n");// send BroadcastNotepad.getActivity().sendBroadcast(intent);}}}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);thread.start();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.i("send", "停止发送广播!");flag = false;}public static Service getService() {return service;}}