IntentService使用
生活随笔
收集整理的這篇文章主要介紹了
IntentService使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
說實話,對于這個類在我實際工作中并沒有用到過,通常也只是用了它的父類Service,通過官方文檔可以看出類的層次結構:
而在今年的一次面試當中,有個面試官提起了它,所以雖說目前還沒有真實在項目中用它,但是有必要先了解一下它的用法,在大腦中有一定的印象,以便將來真正能用到時則直接參考既可。
對于這個類的使用當然不用自己摸索,可以參考該博文:http://blog.csdn.net/hudashi/article/details/7986130
先依照官網的介紹有個大致的了解【來自銀家博文】:
下面來把博文中說的例子給運行看下效果:
先聲明服務MyIntentService:
public class MyIntentService extends IntentService {final static String TAG = "cexo";public MyIntentService() {super("com.example.layouttest.MyIntentService");Log.i(TAG, this + " is constructed");}@Overrideprotected void onHandleIntent(Intent arg0) {Log.i(TAG, "begin onHandleIntent() in " + this);try {Thread.sleep(5 * 1000);} catch (InterruptedException e) {e.printStackTrace();}Log.i(TAG, "end onHandleIntent() in " + this);}public void onDestroy() {super.onDestroy();Log.i(TAG, this + " is destroy");} }然后再主界面去調這個服務MainActivity:
public class MainActivity extends Activity implements OnClickListener {// viewsprivate Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button.setOnClickListener(this);}@Overridepublic void onClick(View arg0) {Intent intent = new Intent(this, MyIntentService.class);startService(intent);// 連續發起了三次請求 startService(intent);startService(intent);}}下面來看下運行的效果:
從運行效果可以發現跟Service的一個很大的區別在于,同時發出多個請求之后,當最后一個請求被處理,則整個Service也退出了,關于它真正的使用場景,待在實際工作中用到了再來總結,目前先學會怎么用它~
?
總結:
在網上搜到了一個關于IntentService的一個特點,我覺得有幾點寫出了它的好處:
轉載于:https://www.cnblogs.com/webor2006/p/4305715.html
總結
以上是生活随笔為你收集整理的IntentService使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第二节 安装CentOS
- 下一篇: 关于visual studio类视图和资