IntentService
IntentSerivce的特征:
1.会创建单独的work来处理所有的Intent请求,
2.会创建单独的work线程来处理onHandleIntent()方法实现的代码
3.当所有请求处理完毕后,Intentservice会自动停止
IntentSerivce相对于Service的优势
1.启动一条单独的新线程
2.不用重写onBind(),onStarcommand()方法
实现过程创建activitActivity本身作为事件监听——>创建MyIntentService——>配置MyIntentService——>创建需要启动的MyIntentService的intent与启动service
主要MainActivity.java代码:
package com.example.intentservicetest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
Button bt=null;
Intent intent=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initi_way();
contry_way();
}
private void contry_way() {
//绑定事件监听
bt.setOnClickListener(this);
}
private void initi_way() {
bt=(Button) findViewById(R.id.int_bt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
//监听触发
public void onClick(View arg0) {
Toast.makeText(this, "hello,intentservice start", Toast.LENGTH_SHORT).show();
//创建需要启动的MyIntentService
intent =new Intent(this,MyIntentService.class);
//启动服务
startService(intent);
}
}
主要的MyIntentService.java代码
package com.example.intentservicetest;
import android.app.IntentService;
import android.content.Intent;
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent arg0) {
for(int i=0;i<100;i++){
System.out.println("第"+i+"项");
}
}
}
主要的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intentservicetest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.intentservicetest.MainActivity"
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="com.example.intentservicetest.MyIntentService"></service>
</application>
</manifest>
1.创建activit与Activity本身作为事件监听
package com.example.intentservicetest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
Button bt=null;
Intent intent=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initi_way();
contry_way();
}
private void contry_way() {
//绑定事件监听
bt.setOnClickListener(this);
}
private void initi_way() {
bt=(Button) findViewById(R.id.int_bt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
//监听触发
public void onClick(View arg0) {
myint_way();
}
2.创建MyIntentService
package com.example.intentservicetest;
import android.app.IntentService;
import android.content.Intent;
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent arg0) {
for(int i=0;i<100;i++){
System.out.println("第"+i+"项");
}
}
}
3.配置MyIntentService
<service
android:name="com.example.intentservicetest.MyIntentService"></service>
4.创建需要启动的MyIntentService的intent与启动service
private void myint_way() {
//创建需要启动的MyIntentService
intent =new Intent(this,MyIntentService.class);
//启动服务
startService(intent);
}
5.运行效果