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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android Service与Runnable整合并用

發布時間:2025/3/15 Android 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Service与Runnable整合并用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

服務的啟動沒有Activity,即便是利用Activity帶起服務,也會有各看成獨立的事件及焦點要處理。

Service繼承自Android.app.Service。

服務的生態鏈就先從onCreate()開始(如果有重寫的話) ,接著應會進入啟動服務onStart(),默認繼承的Service類,并不一定要有onStart(),但是一定要重寫public IBinder onBind(Intent intent)方法。

?package?cn.iimob;


import?android.app.Activity;
import?android.content.Intent;
import?android.os.Bundle;
import?android.view.View;
import?android.widget.Button;

public?class?demo?extends?Activity?{
????private?Button?btnStartService,btnStopService;
????/**?Called?when?the?activity?is?first?created.?*/
????@Override
????public?void?onCreate(Bundle?savedInstanceState)?{
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.main);
????????btnStartService=(Button)findViewById(R.id.btnStartService);
????????btnStopService=(Button)findViewById(R.id.btnStopService);
????????btnStartService.setOnClickListener(new?Button.OnClickListener()?{
????????????@Override
????????????public?void?onClick(View?v)?{
????????????????//構建?Intent?對象,指定打開對象為?MyService服務
????????????????Intent?i=new?Intent(demo.this,?MyService.class);
????????????????//設置新Task的方式
????????????????i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
????????????????//以startService?方法啟動?Intent
????????????????startService(i);
????????????}
????????});
????????btnStopService.setOnClickListener(new?Button.OnClickListener()?{
????????????
????????????@Override
????????????public?void?onClick(View?v)?{
????????????????//?構建?Intent對象,指定關閉的對象為MyService服務
????????????????Intent?i=new?Intent(demo.this,?MyService.class);
????????????????
????????????????//以stopService?方法關閉?Intent
????????????????stopService(i);
????????????}
????????});
????}
}

?package?cn.iimob;


import?android.app.Service;
import?android.content.Intent;
import?android.os.Handler;
import?android.os.IBinder;
import?android.util.Log;

/**
?*?
?*??@Project???????:?servicedemo
?*??@Program?Name??:?cn.iimob.MyService.java
?*??@Class?Name????:?MyService
?*??@Description???:?自定義?MyService?類繼承?Service?類
?*??@Author????????:?zh
?*??@Creation?Date?:?2011-11-3?上午09:49:00?
?*??@ModificationHistory??
?*??Who????????When??????????What?
?*??--------???----------????-----------------------------------
?*??username???2011-11-3???????TODO
?
*/
public?class?MyService?extends?Service?{

????/**
?????*?創建?Handler?對象,作為進程?傳遞?postDelayed?之用
?????
*/
????private?Handler?myhandler?=?new?Handler();
????
????/**
?????*?為了確認系統服務運行情況
?????
*/
????private?int?intCounter=0;
????
????/**
?????*?成員變量?myTasks為Runnable對象,作為Timer之用
?????
*/
????private?Runnable?myTasks=new?Runnable()?{
????????/**
?????????*?進程運行
?????????
*/
????????@Override
????????public?void?run()?{
????????????//?TODO?Auto-generated?method?stub
????????????
//遞增counter整數,作為后臺服務運行時間識別
????????????intCounter++;
????????????//以Log?對象在LogCat?里輸出Log信息,監看服務運行情況
????????????Log.i("Run?Service",?"Counter:"+Integer.toString(intCounter));
????????????myhandler.postDelayed(myTasks,?1000);
????????}
????};
????
????@Override
????public?IBinder?onBind(Intent?intent)?{
????????return?null;
????}
????
????@Override
????public?void?onStart(Intent?intent,int?startId){
????????myhandler.postDelayed(myTasks,?1000);
????????super.onStart(intent,?startId);
????????Log.i("Start?Service",?"onStart");
????}
????
????@Override
????public?void?onCreate(){
????????super.onCreate();
????????Log.i("Create?Service",?"onCreate");
????}
????
????@Override
????public?void?onDestroy(){
????????//當服務結束,刪除?mTasks?運行線程?
????????myhandler.removeCallbacks(myTasks);
????????super.onDestroy();
????????Log.i("Destroy?Service",?"onDestroy");
????}
????
????
}

?

?<?xml?version="1.0"?encoding="utf-8"?>

<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"
????android:orientation
="vertical"
????android:layout_width
="fill_parent"
????android:layout_height
="fill_parent"
????
>
<TextView??
????
android:layout_width="fill_parent"?
????android:layout_height
="wrap_content"?
????android:text
="@string/hello"
????
/>
<Button?android:text="開始Service"?android:id="@+id/btnStartService"?android:layout_width="wrap_content"?android:layout_height="wrap_content"></Button>
<Button?android:text="終止Service"?android:id="@+id/btnStopService"?android:layout_width="wrap_content"?android:layout_height="wrap_content"></Button>
</LinearLayout>

?

?<?xml?version="1.0"?encoding="utf-8"?>

<manifest?xmlns:android="http://schemas.android.com/apk/res/android"
??????package
="cn.iimob"
??????android:versionCode
="1"
??????android:versionName
="1.0">
????<application?android:icon="@drawable/icon"?android:label="@string/app_name">
????????<activity?android:name=".demo"
??????????????????android:label
="@string/app_name">
????????????<intent-filter>
????????????????<action?android:name="android.intent.action.MAIN"?/>
????????????????<category?android:name="android.intent.category.LAUNCHER"?/>
????????????</intent-filter>
????????</activity>
????????<!--?創建?Service,給予類的名稱??-->
????????<!--?創建?android:exported屬情為true,表示此服務可被其他程序訪問??-->
????????<service?android:name=".MyService"?android:exported="true"?android:process=":remote"></service>
????</application>
????<uses-sdk?android:minSdkVersion="8"?/>

</manifest>?

轉載于:https://www.cnblogs.com/skyblue/archive/2011/11/03/2234252.html

總結

以上是生活随笔為你收集整理的Android Service与Runnable整合并用的全部內容,希望文章能夠幫你解決所遇到的問題。

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