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. 短视频SDK简单易用——来自RDSDK.COM

    锐动天地为开发者提供短视频编辑.视频直播.特效.录屏.编解码.视频转换,等多种解决方案,涵盖PC.iOS.Android多平台.以市场为导向,不断打磨并创新技术,在稳定性,兼容性,硬件设备效率优化上千 ...

  2. 最新最强短视频SDK——来自RDSDK.COM

    北京锐动天地信息技术有限公司成立于2007年9月.多年来一直专注于音视频领域核心技术的研发, 拥有Windows.iOS.Android全平台自主知识产权的领先技术产品. 2011年获得新浪战略投资, ...

  3. CAD参数绘制线型标注(com接口)

    主要用到函数说明: _DMxDrawX::DrawDimRotated 绘制一个线型标注.详细说明如下: 参数 说明 DOUBLE dExtLine1PointX 输入第一条界线的起始点X值 DOUB ...

  4. java线程学习2

    sleep  变为阻塞态  但不释放锁  休眠指定毫秒时间 yield  变为就绪态  可能立即被执行  也可能不立即被执行 join   插队  暂停当前执行的线程  让调用join的线程先执行 线 ...

  5. Android反编译初步

    网上关于Android反编译的帖子很多,反编译的步骤也是很详细,本文Android反编译参考博客:https://www.cnblogs.com/dhcn/p/7120891.html 而反编译中最主 ...

  6. 小程序调用支付报错:jsapi缺少参数: total_fee

    这种情况通常是因为在调用的时候,package参数有问题导致: wx.requestPayment中package参数必须是package:"prepay_id=wx21********** ...

  7. JS 水印图片合成实例页面

    CSS代码: .clip { position: absolute; clip: rect(0 0 0 0); } HTML代码: <input type="file" id ...

  8. Dash Speed

    题目大意: 比特山是比特镇的飙车圣地.在比特山上一共有n 个广场,编号依次为1 到n,这些广场之间通过n - 1 条双向车道直接或间接地连接在一起,形成了一棵树的结构.因为每条车道的修建时间以及建筑材 ...

  9. 笔试算法题(33):烙饼排序问题 & N!阶乘十进制末尾0的个数二进制最低1的位置

    出题:不同大小烙饼的排序问题:对于N块大小不一的烙饼,上下累在一起,由于一只手托着所有的饼,所以仅有一只手可以翻转饼(假设手足够大可以翻转任意块数的 饼),规定所有的大饼都出现在小饼的下面则说明已经排 ...

  10. 树莓派 -- bcm2835 library (1)

    bcm2835 library提供了user space 操作IO的代码. 本文不涉及代码分析,先直观的按照user guide完成操作. 1. 在Raspberry中安装bcm2835 librar ...