怎樣用service后臺(tái)開(kāi)線程及廣播做一個(gè)計(jì)時(shí)器?這篇文章分別介紹了后臺(tái)開(kāi)線程和廣播做一個(gè)計(jì)時(shí)器的方法,代碼示例非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
我們提供的服務(wù)有:成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、龍城ssl等。為超過(guò)千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的龍城網(wǎng)站制作公司
1.首先寫(xiě)一個(gè)服務(wù),在onStartCommand方法里開(kāi)啟線程,每次startService就會(huì)調(diào)一次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就是我們要發(fā)送的內(nèi)容
Runnable runnable = new Runnable() {
@Override
public void run() {
Log.e(TAG, "線程," + "倒計(jì)時(shí)=" + lastTime);
handler.postDelayed(this, 1000);// 50是延時(shí)時(shí)長(zhǎng)
lastTime = lastTime - 1;
intent.putExtra("time", lastTime);
intent.setAction(Configs.Time_Action); // 設(shè)置你這個(gè)廣播的action,只有和這個(gè)action一樣的接受者才能接受者才能接收廣播
sendBroadcast(intent);
if (lastTime <= 0) {
Log.e(TAG, "結(jié)束.....");
handler.removeCallbacks(this);
running = false;
}
}
};
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
// 這里可以啟動(dòng)媒體播放器
// 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" + "開(kāi)始倒計(jì)時(shí)=" + lastTime);
return START_STICKY;
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
// handler.removeCallbacks(runnable);
stopSelf();
super.onDestroy();
}
// 定義內(nèi)容類(lèi)繼承Binder
public class LocalBinder extends Binder {
// 返回本地服務(wù)
public LocalService getService() {
return LocalService.this;
}
}
}
2.在清單文件里面注冊(cè)廣播
3.在activity或者片段里面寫(xiě)廣播,接收條件,注冊(cè)廣播,記得推出界面后注銷(xiāo)廣播
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) {//在這里面接收信息
}
}
//注銷(xiāo)廣播
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
getActivity().unregisterReceiver(timeReceiver);
}
4.開(kāi)啟廣播,可以帶入信息,第一次會(huì)啟動(dòng)onStart,此后不再啟動(dòng)onStart,但onStartCommand每次都會(huì)開(kāi)啟:
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);
}
以上就是service后臺(tái)開(kāi)線程及廣播做一個(gè)計(jì)時(shí)器的具體操作,代碼應(yīng)該是足夠清楚的,而且我也相信有相當(dāng)?shù)囊恍├涌赡苁俏覀內(nèi)粘9ぷ骺赡軙?huì)見(jiàn)得到的。通過(guò)這篇文章,希望你能收獲更多。