android static实现Service与Activity中Handler通信

第一种方式:从Service中获取Activity的static变量,调用该静态变量的getHandler()获取Handler进行发送消息

MainActivity:

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.content.IntentCompat;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

Handler handler;
//static Handler handler;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	
	context = getApplicationContext();
	
	handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			String string = (String)msg.obj;
			Log.e("Log",string);
			super.handleMessage(msg);
		}
	};

	TestService.startSevice(MainActivity.this);
	
	Log.e("Log","TestService start ok" );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
	getMenuInflater().inflate(R.menu.main, menu);
	return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	int id = item.getItemId();
	if (id == R.id.action_settings) {
		return true;
	}
	return super.onOptionsItemSelected(item);
}

public Handler getHandler(){
	return this.handler;
}

public Context getContext(){
	return this.context;
}

}

TestService:
package com.example.test;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.content.IntentCompat;
import android.util.Log;

public class TestService extends Service {

private static MainActivity mainActivity;

static void startSevice(Context c) {
	mainActivity = (MainActivity) c;
	Intent in = new Intent(c, TestService.class);
	c.startService(in);
}

@Override
public IBinder onBind(Intent intent) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public void onCreate() {
	Log.e("Log","TestService start ok2" );
	super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
	//new TestThread(mainActivity).start();
	new TestThread(mainActivity).start();

	return super.onStartCommand(intent, flags, startId);
}

class TestThread extends Thread {
	private Context context;
	private Handler handler;
	public TestThread(){
		
	}

	public TestThread(Context context) {
		this.context = context;
		this.handler = ((MainActivity) context).getHandler();
	}

	@Override
	public void run() {
		Message msg = new Message();
		msg.obj = "HelloWorld!";
		handler.sendMessage(msg);
		//MainActivity.handler.sendMessage(msg);
		super.run();
	}
}

}

第二种方式:将Activity中的Handler设置为static,然后直接在Service中调用Activity的静态Handler的进行发送消息
MainActivity:

package com.example.test;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.content.IntentCompat;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

//Handler handler;
static Handler handler;
//Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	
	//context = getApplicationContext();
	
	handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			String string = (String)msg.obj;
			Log.e("Log",string);
			super.handleMessage(msg);
		}
	};

	//TestService.startSevice(MainActivity.this);
	Intent intent = new Intent(MainActivity.this,TestService.class);
	startService(intent);
	
	Log.e("Log","TestService start ok" );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
	getMenuInflater().inflate(R.menu.main, menu);
	return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	int id = item.getItemId();
	if (id == R.id.action_settings) {
		return true;
	}
	return super.onOptionsItemSelected(item);
}

public Handler getHandler(){
	return this.handler;
}

//public Context getContext(){
//	return this.context;
//}

}

TestService:
package com.example.test;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v4.content.IntentCompat;
import android.util.Log;

public class TestService extends Service {

//private static MainActivity mainActivity;

//static void startSevice(Context c) {
//	mainActivity = (MainActivity) c;
//	Intent in = new Intent(c, TestService.class);
//	c.startService(in);
//}

@Override
public IBinder onBind(Intent intent) {
	// TODO Auto-generated method stub
	return null;
}

@Override
public void onCreate() {
	Log.e("Log","TestService start ok2" );
	super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
	//new TestThread(mainActivity).start();
	new TestThread().start();

	return super.onStartCommand(intent, flags, startId);
}

class TestThread extends Thread {
	private Context context;
	private Handler handler;
	public TestThread(){
		
	}

	public TestThread(Context context) {
		this.context = context;
		this.handler = ((MainActivity) context).getHandler();
	}

	@Override
	public void run() {
		Message msg = new Message();
		msg.obj = "HelloWorld!";
		//handler.sendMessage(msg);
		MainActivity.handler.sendMessage(msg);
		super.run();
	}
}

}


你可能感兴趣的:(安卓开发)