标签:计时器
1.首先写一个服务,在onStartCommand方法里开启线程,每次startService就会调一次onStartCommand方法
import java.util.List;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class LocalService extends Service {
public static final String ACTION = "com.happyparking.service.LocalService";
private static final String TAG = "LocalService";
private IBinder binder = new LocalService.LocalBinder();
final Handler handler = new Handler();
int lastTime = 0;
boolean running = false;
Intent intent = new Intent(); // Itent就是我们要发送的内容
Runnable runnable = new Runnable() {
@Override
public void run() {
Log.e(TAG, "线程," + "倒计时=" + lastTime);
handler.postDelayed(this, 1000);// 50是延时时长
lastTime = lastTime - 1;
intent.putExtra("time", lastTime);
intent.setAction(Configs.Time_Action); // 设置你这个广播的action,只有和这个action一样的接受者才能接受者才能接收广播
sendBroadcast(intent);
if (lastTime <= 0) {
Log.e(TAG, "结束.....");
handler.removeCallbacks(this);
running = false;
}
}
};
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
// 这里可以启动媒体播放器
// if(mediaPlayer==null)
// mediaPlayer=MediaPlayer.create(this, uri);
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
if (running)
handler.removeCallbacks(runnable);
if(intent!=null)
lastTime = intent.getIntExtra("time", 0);
handler.postDelayed(runnable, 1000);
running = true;
Log.e(TAG, "onStart" + "开始倒计时=" + lastTime);
return START_STICKY;
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
// handler.removeCallbacks(runnable);
stopSelf();
super.onDestroy();
}
// 定义内容类继承Binder
public class LocalBinder extends Binder {
// 返回本地服务
public LocalService getService() {
return LocalService.this;
}
}
}
2.在清单文件里面注册广播
<service android:name="com.happyparking.service.LocalService" >
<intent-filter>
<action android:name="com.happyparking.service.LocalService" />
</intent-filter>
</service>
3.在activity或者片段里面写广播,接收条件,注册广播,记得推出界面后注销广播
private void initReceiver() {
timeReceiver = new TimeReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(Configs.Time_Action); // 只有持有相同的action的接受者才能接收此广播
getActivity().registerReceiver(timeReceiver, filter);
}
public class TimeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
if (intent != null) {//在这里面接收信息
}
}
//注销广播
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
getActivity().unregisterReceiver(timeReceiver);
}
4.开启广播,可以带入信息,第一次会启动onStart,此后不再启动onStart,但onStartCommand每次都会开启:
public void sendMyService() {
Intent service = new Intent(LocalService.ACTION);//LocalService.ACTION是广播条件
service.putExtra("time", second);//带入信息,
// ((MainActivity)content).startMyService(10,(TextView)mMenuView.findViewById(R.id.tvTime));
content.startService(service);
}
标签:计时器
原文地址:http://9105034.blog.51cto.com/9095034/1680688