日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IntentService使用

發布時間:2023/12/9 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。