package com.pingyijinren.test;

import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.util.Log; /**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
* <p/>
* TODO: Customize class - update intent actions, extra parameters and static
* helper methods.
*/
public class MyIntentService extends IntentService {
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
private static final String ACTION_FOO = "com.pingyijinren.test.action.FOO";
private static final String ACTION_BAZ = "com.pingyijinren.test.action.BAZ"; // TODO: Rename parameters
private static final String EXTRA_PARAM1 = "com.pingyijinren.test.extra.PARAM1";
private static final String EXTRA_PARAM2 = "com.pingyijinren.test.extra.PARAM2"; public MyIntentService() {
super("MyIntentService");
} /**
* Starts this service to perform action Foo with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
// TODO: Customize helper method
public static void startActionFoo(Context context, String param1, String param2) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_FOO);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
} /**
* Starts this service to perform action Baz with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
// TODO: Customize helper method
public static void startActionBaz(Context context, String param1, String param2) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_BAZ);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
} @Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_FOO.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionFoo(param1, param2);
} else if (ACTION_BAZ.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionBaz(param1, param2);
}
}
Log.d("MainActivity","我是子线程"+Thread.currentThread().getId()+"");
} /**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
// TODO: Handle action Foo
throw new UnsupportedOperationException("Not yet implemented");
} /**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
throw new UnsupportedOperationException("Not yet implemented");
} @Override
public void onDestroy(){
super.onDestroy();
Log.d("MainActivity","destroy");
}
}
package com.pingyijinren.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity{
private Button startIntentService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); startIntentService=(Button)findViewById(R.id.startIntentService); startIntentService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MainActivity","我是主线程"+Thread.currentThread().getId());
Intent intent=new Intent(MainActivity.this,MyIntentService.class);
startService(intent);
}
});
}
}

IntentService用于服务中开启子线程的自动关闭的更多相关文章

  1. iOS中使用子线程的完整方法

    http://www.cnblogs.com/ygm900/archive/2013/06/23/3151691.html 第一步:开启子线程 //开启子线程到网络上获取数据 myFirstThrea ...

  2. 2017-11-29 由runnable说起Android中的子线程和主线程

    1.首先纠正一个观点,就是runnable运行在子线程中是错误的观念.runnable只是创建了一个执行任务的对象,但是它本身并不会创建一个新的子线程,Runable只是给你接口让你实现工作线程的工作 ...

  3. java方法中开启一个线程

    很多业务场景下需要你在一个方法中去开启一个线程,去跑一些处理时间较长的代码,这样调用方就不必经过长时间的等待了.好了 话不多说  先上代码: package test; public class Th ...

  4. Android中,子线程使用主线程中的组件出现问题的解决方法

    Android中,主线程中的组件,不能被子线程调用,否则就会出现异常. 这里所使用的方法就是利用Handler类中的Callback(),接受线程中的Message类发来的消息,然后把所要在线程中执行 ...

  5. C#中,子线程与主线程之间的通信是如何实现(转)

    注: 项目中按照这个方法调试成功: 通常我们会有这种需求: 一个支持慢速设备的处理类,如网络通信.串口通信.打印等 此时经常需要将线程封装在类里面,让类支持异步处理,然后发布事件或者回调委托通知主线程 ...

  6. 子线程导致 Windows 服务停止的情况(Topshelf 结合 Quartz.NET)

    Ø  前言 本文主要记录子线程导致 Topshelf 和 Quartz.NET 的 Windows 服务停止的现象,以及使用几种常用子线程的注意事项.因为我们有时可能需要开启多个线程执行复杂的逻辑,如 ...

  7. 【转载】Delphi7从子线程中发送消息到主线程触发事件执行

    在对数据库的操作时,有时要用一个子线程来进行后台的数据操作.比如说数据备份,转档什么的.在主窗口还能同是进行其它操作.而有时后台每处理一个数据文件,要向主窗口发送消息,让主窗口实时显示处理进度在窗口上 ...

  8. c/c++中主线程退出,子线程也会退出

    #include <windows.h> #include <process.h> /* _beginthread, _endthread */ #include <io ...

  9. vc中主线程等待子线程退出的方法

    VC线程同步,在子线程中等待另一子线程结束,通过WaitForSingleObject可以实现,但是如果在主线程中等待子线程结束,这个函数是无法完成要求的,因为它会造成主线程挂起,导致程序死掉.我们可 ...

随机推荐

  1. AJPFX关于modifier总结

    修饰符总结 Modifiers        函数修饰符始终在返回值类型之前!!!        变量修饰符始终在变量类型之前!!!---------------------------------- ...

  2. 来自AJPFX的二分法查找

    package com.heima.array; public class Demo2_Array { /**         * * A:案例演示                        * ...

  3. Android EventBus3.0详解

    修改日志 -- 添加索引部分得细节,添加kotlin的支持方式 https://www.jianshu.com/p/31e3528ca7e5

  4. 掌握Spark机器学习库-07-回归分析概述

    1)回归与分类算法的区别 回归的预测结果是连续的,分类的预测结果是离散的. 2)spark实现的回归算法有: 3)通过相关系数衡量线性关系的程度

  5. idea 常用操作

    1.创建的maven项目,java文件不提示错误:有main方法但右击却找不到run选项的问题 1)首先要配置SDK--就是配置JDK 2)然后要按照提示信息导入某些maven相关的东西,就这个Eve ...

  6. cp - 复制文件和目录

    总览 cp [选项] 文件路径 cp [选项] 文件...目录 POSIX 选项: [-fipRr] GNU 参数(最短形式): [-abdfilprsuvxPR] [-S SUFFIX] [-V { ...

  7. js 删除数组中某一项的几种方法总结

    第一种:改变原数组 借用原生数组方法:splice(index,len,[item])  剪接 借用原生对象方法:delete array[index] + array.slice(0, index) ...

  8. 打开windows服务

    #include <winsvc.h> void CXXXDlg::ServiceRun() { SERVICE_STATUS ssStatus; //获得ServiceControl M ...

  9. Android获取屏幕的大小与密度的代码

    Android项目开发中很多时候需要获取手机屏幕的宽高以及屏幕密度来进行动态布局,这里总结了三种获取屏幕大小和屏幕密度的方法 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  10. P1387 最大正方形&&P1736 创意吃鱼法

    P1387 最大正方形 P1736 创意吃鱼法 两道类似的$DP$ 转移方程基本上类似于$f[i][j]=min(f[i-1][j-1],min(f[i][j-1],f[i-1][j]))$ 考虑构成 ...