首先建立一个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的运用的更多相关文章

  1. Android 多线程:使用Thread和Handler

    当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主要负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事件以及屏幕绘图事件,并把相关的事件分 ...

  2. Android 多线程:使用Thread和Handler (从网络上获取图片)

    当一个程序第一次启动时,Android会同时启动一个对应的主线程(Main Thread),主线程主要负责处理与UI相关的事件,如:用户的按键事件,用户接触屏幕的事件以及屏幕绘图事件,并把相关的事件分 ...

  3. Android多线程全面解析:IntentService用法&源码

    前言 多线程的应用在Android开发中是非常常见的,常用方法主要有: 继承Thread类 实现Runnable接口 AsyncTask Handler HandlerThread IntentSer ...

  4. Android多线程分析之二:Thread的实现

    Android多线程分析之二:Thread的实现 罗朝辉 (http://www.cnblogs.com/kesalin/) CC 许可,转载请注明出处   在前文<Android多线程分析之一 ...

  5. Android多线程分析之一:使用Thread异步下载图像

    Android多线程分析之一:使用Thread异步下载图像 罗朝辉 (http://www.cnblogs.com/kesalin) CC 许可,转载请注明出处   打算整理一下对 Android F ...

  6. Android多线程分析之中的一个:使用Thread异步下载图像

    Android多线程分析之中的一个:使用Thread异步下载图像 罗朝辉 (http://blog.csdn.net/kesalin) CC 许可.转载请注明出处 打算整理一下对 Android Fr ...

  7. java多线程以及Android多线程

    Java 多线程 线程和进程的区别 线程和进程的本质:由CPU进行调度的并发式执行任务,多个任务被快速轮换执行,使得宏观上具有多个线程或者进程同时执行的效果. 进程:在操作系统来说,一个运行的程序或者 ...

  8. android多线程-AsyncTask之工作原理深入解析(下)

    关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...

  9. android多线程-AsyncTask之工作原理深入解析(上)

    关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...

  10. Android线程管理之Thread使用总结

    前言 最近在一直准备总结一下Android上的线程管理,今天先来总结一下Thread使用. 线程管理相关文章地址: Android线程管理之Thread使用总结 Android线程管理之Executo ...

随机推荐

  1. .Net Core 中使用AutoMapper

    1.新建一个类 using AutoMapper; using YourModels; using YourViewModels; namespace YourNamespace { public c ...

  2. chapter 13_3 table访问的元方法

    前两节的算术类.关系类运算符的元方法都为各种错误情况定义了行为,它们不会改变语言的常规行为. 但是Lua还提供了两种可以改变table行为的方法: 一种是查询table中不存在的字段.一种是修改tab ...

  3. iOS页面间传值的六种方式

    一般ios页面间的传值方式分为6种:1.属性传值:2.block:3.delegate:4.UserDefault:5.单例:6.通知. 0&1.block 先说我最常用的block吧,属性传 ...

  4. 解决 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死锁问题

    在我们将站点从 ASP.NET + Windows 迁移至 ASP.NET Core + Linux 的过程中,目前遇到的最大障碍就是 —— 没有可用的支持 .NET Core 的 memcached ...

  5. do循环的100米自由落体

    #include "stdio.h" void main() { float h=100.0,sum=100.0; ; do { sum=sum+h; h=h/; g++; }); ...

  6. postgresql删除属性

    PostgreSQL update and delete property from JSONB column up vote 2 down vote favorite From this artic ...

  7. Hadoop上的中文分词与词频统计实践 (有待学习 http://www.cnblogs.com/jiejue/archive/2012/12/16/2820788.html)

    解决问题的方案 Hadoop上的中文分词与词频统计实践 首先来推荐相关材料:http://xiaoxia.org/2011/12/18/map-reduce-program-of-rmm-word-c ...

  8. POJ 2418 Hardwood Species (哈希,%f 和 %lf)

    我的错因: 本来改用%f输出,我用了%lf,结果编译器直接判定为错误(一部分编译器认为lf是没有错的).当时我还以为是hash出错了.. 方法不止一种: 方法 时间   空间 Hash 891ms 5 ...

  9. 11--Python 备份文件程序

    最近看了下<A Byte of Python>, 看见一个非常有意思的程序,用python进行文件行备份的练习程序, 自己在机器上敲代码运行了一遍,结果出现了一个小问题,路径出错--&qu ...

  10. 4-static(静态变量)关键字

    1.使用static声明属性 static声明全局属性2.使用static声明方法 直接通过类名调用3.注意点 使用static方法的时候,只能访问static声明的属性和方法,而非static声明的 ...