前段时间做项目,用到service了,当时只是匆匆忙忙的用了,具体什么原理也不知道,项目上线了,终于有时间学习了,小菜鸟终于可以整理一下
根据程序来看一下service的生命周期(由于布局只有几个按钮,这里就不贴图了)
1.MainActivity.java
package com.sdufe.thea.guo; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button button1, button2, button3, button4, button5, button6; private String TAG = "MainActivity"; private ServiceConnection conn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "onServiceDisconnected"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(TAG, "onServiceConnected"); } }; } private void initView() { button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); button4 = (Button) findViewById(R.id.button4); button5 = (Button) findViewById(R.id.button5); button6 = (Button) findViewById(R.id.button6); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); button4.setOnClickListener(this); button5.setOnClickListener(this); button6.setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = new Intent(MyService.ACTION); switch (v.getId()) { // 启动服务 case R.id.button1: startService(intent); break; // 绑定服务 case R.id.button2: bindService(intent, conn, BIND_AUTO_CREATE); break; // 解除服务 case R.id.button3: unbindService(conn); break; // 结束服务 case R.id.button4: stopService(intent); break; // myservice case R.id.button5: startService(new Intent(this, MyService.class)); break; // myintentservice case R.id.button6: startService(new Intent(this, MyIntentService.class)); break; } } }布局中存在六个button,其中前四个分别是测试service的生命周期的,最后两个测试service的,由于service不在单独的进程中,又不是线程,还有就是我们习惯性把耗时的操作放在service中,所以来测试一下service和intentService
2.MyService.java
package com.sdufe.thea.guo; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; public class MyService extends Service { private String TAG="MyService"; public static final String ACTION="com.sdufe.thea.guo.MyService"; @Override public IBinder onBind(Intent intent) { Log.d(TAG, TAG+"onBind"); return null; } @Override public void onCreate() { Log.d(TAG, TAG+"onCreate"); super.onCreate(); } @Override @Deprecated public void onStart(Intent intent, int startId) { Log.d(TAG, TAG+"onStart"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } Log.d(TAG, TAG+"睡眠结束"); super.onStart(intent, startId); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, TAG+"onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public boolean onUnbind(Intent intent) { Log.d(TAG, TAG+"onUnbind"); return super.onUnbind(intent); } @Override public void onDestroy() { Log.d(TAG, TAG+"onDestroy"); super.onDestroy(); } }
3.在AndroidManifest.xml给service注册一下
<service android:name="com.sdufe.thea.guo.MyService" > <intent-filter> <action android:name="com.sdufe.thea.guo.MyService" /> </intent-filter> </service>
点击结束服务:
点击绑定服务:
结束服务:
注:服务都是成对出现
下面来说一下Service和IntentService,一般费时的操作会放在service,但是当时间长时,会出现Application Not Responding
下面呈上service和IntentService源码,一看就懂了
public abstract class Service extends ContextWrapper implements ComponentCallbacks2 { private static final String TAG = "Service"; public Service() { super(null); }
}
public abstract class IntentService extends Service { private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; private String mName; private boolean mRedelivery; private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); stopSelf(msg.arg1); } }
}
很多时候服务和通知会一起用,这里也顺便写了写通知,代码如下:
package com.sdufe.thea.guo; import android.app.IntentService; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.util.Log; public class MyIntentService extends IntentService { private String TAG = "MyIntentService"; private NotificationManager notificationManager; public MyIntentService() { super("***"); } @Override protected void onHandleIntent(Intent intent) { Log.d(TAG, TAG + "onStart"); try { Thread.sleep(30000); } catch (InterruptedException e) { e.printStackTrace(); } notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); @SuppressWarnings("deprecation") Notification notification = new Notification(R.drawable.ic_launcher, "通知", System.currentTimeMillis()); intent = new Intent(getApplicationContext(), SecondActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity( getApplicationContext(), 0, intent, 0); notification.setLatestEventInfo(getApplicationContext(), "标题", "内容...", pendingIntent); notificationManager.notify(0, notification); Log.d(TAG, TAG + "睡眠结束"); } }
ok,差不多先分享这些
源码地址:http://download.csdn.net/detail/elinavampire/8115975