您好,登录后才能下订单哦!
怎样用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。