Android—IntentService

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);






發佈了58 篇原創文章 · 獲贊 4 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章