Android----->多線程的實現Thread、IntentService的運用

來源:互聯網
上載者:User

標籤:

 

 

 

首先建立一個Intent.xml

 

<?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"    >    <Button        android:id="@+id/btnStartNormalService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Start NormalService"        />    <Button        android:id="@+id/btnStartIntentService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Start IntentService"        /></LinearLayout>

 

建立一個MyService.java 實現Thread多線程

package com.szy.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;public class MyService extends Service{    protected static final String TAG = "IntentActivity";    @Override    public void onCreate()    {        super.onCreate();    }    @Override    public void onDestroy()    {        super.onDestroy();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId)    {        new MyThread().start();        return START_STICKY;    }    @Override    public IBinder onBind(Intent intent)    {        return null;    }        private class MyThread extends Thread    {        @Override        public void run()        {            try            {                Log.i(TAG, "MyService線程ID:"+Thread.currentThread().getId());                Log.i(TAG, "檔案下載....");                Thread.sleep(2000);            } catch (InterruptedException e)            {                e.printStackTrace();            }                    }            }}

 

再建立一個ExampleIntentService.java的IntentService

package com.szy.service;import android.app.IntentService;import android.content.Intent;import android.util.Log;public class ExampleIntentService extends IntentService{    protected static final String TAG = "IntentActivity";        public ExampleIntentService()    {        super("ExampleIntentService");    }    @Override    protected void onHandleIntent(Intent intent)    {                try        {            Log.i(TAG, "MyService線程ID:"+Thread.currentThread().getId());            Log.i(TAG, "檔案下載....");            Thread.sleep(2000);        } catch (InterruptedException e)        {            e.printStackTrace();        }    }}

 

最後還有建立一個IntentActivity.xml 的Activity

package com.szy.service;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class IntentActivity extends Activity{    protected static final String TAG = "IntentActivity";        private Button btnStartNormalService;    private Button btnStartIntentService;    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.intent);        btnStartNormalService = (Button) findViewById(R.id.btnStartNormalService);        btnStartIntentService = (Button) findViewById(R.id.btnStartIntentService);        btnStartNormalService.setOnClickListener(listener);        btnStartIntentService.setOnClickListener(listener);    }        private OnClickListener listener=new OnClickListener()    {                    public void onClick(View v)        {            Intent intent;            switch (v.getId())            {            case R.id.btnStartNormalService:                intent=new Intent(IntentActivity.this, MyService.class);                Log.i(TAG, "主線程ID:"+Thread.currentThread().getId());                startService(intent);                break;            case R.id.btnStartIntentService:                intent=new Intent(IntentActivity.this, ExampleIntentService.class);                Log.i(TAG, "主線程ID:"+Thread.currentThread().getId());                startService(intent);                break;            default:                break;            }                    }    };}

 

記得修改AndroidManifest.mxl

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.szy.service"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".MainActivity"                  android:label="@string/app_name">        </activity>                <activity android:name=".BinderActivity"                  android:label="@string/app_name">        </activity>                <activity android:name=".IntentActivity"                  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:name=".ExampleService" />        <service android:name=".BinderService" />        <service android:name=".MyService"/>        <service android:name=".ExampleIntentService"/>    </application></manifest>

 

Android----->多線程的實現Thread、IntentService的運用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.