您的位置:首页 > 移动开发 > Android开发

Android—IntentService

2015-08-14 11:43 357 查看
IntentService源码下载http://download.csdn.net/detail/catchingsun/9004273

转载请注明出处 http://blog.csdn.net/catchingsun/article/details/47659211
首先需要先在AndroidManifest.xml中进行配置,如果是多个工程关联则需要在主工程AndroidManifest.xml中进行配置,否则,无任何作用

<service android:name="com.dfd.fec.PositionCoordinateService">
<intent-filter >
<action android:name="PositionCoordinateService"/>
</intent-filter>
</service>
新建Service类,可以在onHandleIntent(Intent intent)方法中书写自己的代码

package com.dfd.fec;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

public class PositionCoordinateService extends IntentService {

private final int xike = 0x0000001;
public PositionCoordinateService() {
super("PositionCoordinateService");

}

@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return super.onBind(intent);
}

@Override
public void onStart(Intent intent, int startId) {
System.out.println("onStart");
super.onStart(intent, startId);
}

@Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();
}

@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Override
public void setIntentRedelivery(boolean enabled) {
System.out.println("setIntentRedelivery");
super.setIntentRedelivery(enabled);
}

@Override
protected void onHandleIntent(Intent intent) {
System.out.println("onHandleIntent");
Bundle getbundle = intent.getExtras();
if (getbundle != null) {
//System.out.println("onHandleIntent");
}
}
}
在Activity中开启服务器:

package com.example.dgfvghvg;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
// private static String TAG = WebMap.class.getName();
private WebView mWebView;
private WebSettings mWebSettings;
private Dialog mphotodialog;
private int random = new Random().nextInt();// 产生随机数
private Intent positioncoordinateintent;
String location = null;
int abscissa = -1;
int ordinate = -1;
/** */
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
start();
}

private void start() {
positioncoordinateintent = new Intent("PositionCoordinateService");
startService(positioncoordinateintent);
}

}
一下代码需要读者自己书写,测试

可以在onHandleIntent(Intent intent)方法中获取Activity传的值

<pre name="code" class="java">Bundle getbundle = intent.getExtras();
String location = getbundle.getString("location");
int abscissa = getbundle.getInt("abscissa");
int ordinate = getbundle.getInt("ordinate");



在Activity中通过Bundle传值至IntentService

Bundle b = new Bundle();
b.putString("location", location);
b.putInt("abscissa", abscissa);
b.putInt("ordinate", ordinate);
//启动服务
positioncoordinateintent = new Intent(AnalyseQRcode.this,PositionCoordinateService.class);
positioncoordinateintent.putExtras(b);
startService(positioncoordinateintent);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: