Android四大主件之服务Service
2015-09-08 18:52
405 查看
概述:
服务是需要注册的,它会在系统后台运行,即使是停止一个活动,服务仍会运行 另外,活动不宜进行耗时的操作,耗时操作应该放在一个单独的线程里。启动服务要用到Intent。
代码:
public class TestServiceActivity extends Activity implements View.OnClickListener{ private Button mButtonStartService; private Button mButtonStopService; private Button mButtonStartDownload; public static final String DOWNLOAD_SERVICE = "com.download.test"; private ProgressBar mProgressBar; private MyReceiver mReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_service); mButtonStartService = (Button) findViewById(R.id.button_start_service); mButtonStopService = (Button) findViewById(R.id.button_stop_service); mButtonStartDownload = (Button) findViewById(R.id.button_start_download); mProgressBar = (ProgressBar) findViewById(R.id.progress_bar); mReceiver = new MyReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(DOWNLOAD_SERVICE); registerReceiver(mReceiver,filter); mButtonStopService.setOnClickListener(this); mButtonStartService.setOnClickListener(this); mButtonStartDownload.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button_start_service: Intent intent = new Intent(getApplicationContext(), MyReceiver.class); startService(intent); break; case R.id.button_stop_service: Intent intent1 = new Intent(getApplicationContext(), MyService.class); stopService(intent1); break; case R.id.button_start_download: Intent intent2 = new Intent(DOWNLOAD_SERVICE); startService(intent2); break; default: break; } } class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { int count = intent.getIntExtra("count",0); mProgressBar.setProgress(count); } } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver); } }
Service与IntentService
public class MyService extends Service { @Override public void onCreate() { super.onCreate(); Log.d("service","onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("service", "onStartCommand"); new Thread(new Runnable() { int count = 0; @Override public void run() { while (count<100){ count++; Intent intent = new Intent(); //intent.setAction(TestServiceActivity.DOWNLOAD_SERVICE); intent.putExtra("count",count); sendBroadcast(intent); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.d("service","onDestroy"); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { return null; } }
public class MyIntentService extends IntentService { int count = 0; /** * Creates an IntentService. Invoked by your subclass's constructor. * * @param name Used to name the worker thread, important only for debugging. */ public MyIntentService(String name) { super(name); } //必须这么写,不然不能在manifests里面注册 public MyIntentService() { super(null); } @Override protected void onHandleIntent(Intent intent) { while (count<100){ count++; Intent intent1 = new Intent(); intent.setAction(TestServiceActivity.DOWNLOAD_SERVICE); intent.putExtra("count",count); sendBroadcast(intent1); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } } }
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button_start_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" android:text="Start Service"/> <Button android:id="@+id/button_stop_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop Service"/> <ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" style="?android:attr/progressBarStyleHorizontal" android:layout_height="10dp" android:layout_margin="10dp"/> <Button android:id="@+id/button_start_download" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start Download"/> </LinearLayout>
相关文章推荐
- Android笔记(二十五)发送自定义广播和本地广播
- Android学习笔记之:获取Android手机(系统)Root权限介绍
- Android四大主件之广播BroadcastProvider
- Android的selector,Button按钮背景选择器
- android监听器事件
- Android之内容提供器ContentProvider的简单表示
- android开发步步为营之73:Android开发常见问题技术点总结之二
- Android 按钮点击切换背景,同时修改文字颜色
- android日志工具类
- 【转】Android 显示原理简介
- android include merge标签
- android工具集合与api参考
- Android iphone手机分辨率尺寸比例整理
- android 上面一个listview下面一个button,让button一直处于listview的下方
- 基于飞思卡尔imx6 sabrelite开发板的android lvds屏幕驱动调试
- Android编程实用小技巧
- 夯实java基础,深入理解Android设计思想
- ListView中的android:transcriptMode属性
- android 银联支付接入报nullexception异常
- Android 性能优化的一些建议