您好,登錄后才能下訂單哦!
怎樣用service后臺開線程及廣播做一個計時器?這篇文章分別介紹了后臺開線程和廣播做一個計時器的方法,代碼示例非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
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);
}
以上就是service后臺開線程及廣播做一個計時器的具體操作,代碼應該是足夠清楚的,而且我也相信有相當的一些例子可能是我們日常工作可能會見得到的。通過這篇文章,希望你能收獲更多。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。