您好,登錄后才能下訂單哦!
怎樣用service后臺開線程及廣播做一個計時器?這篇文章分別介紹了后臺開線程和廣播做一個計時器的方法,代碼示例非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
1.首先寫一個服務(wù),在onStartCommand方法里開啟線程,每次startService就會調(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, "線程," + "倒計時=" + lastTime);
handler.postDelayed(this, 1000);// 50是延時時長
lastTime = lastTime - 1;
intent.putExtra("time", lastTime);
intent.setAction(Configs.Time_Action); // 設(shè)置你這個廣播的action,只有和這個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");
// 這里可以啟動媒體播放器
// 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();
}
// 定義內(nèi)容類繼承Binder
public class LocalBinder extends Binder {
// 返回本地服務(wù)
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);
}
以上就是service后臺開線程及廣播做一個計時器的具體操作,代碼應(yīng)該是足夠清楚的,而且我也相信有相當(dāng)?shù)囊恍├涌赡苁俏覀內(nèi)粘9ぷ骺赡軙姷玫降摹Mㄟ^這篇文章,希望你能收獲更多。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。