用于对static的理解,仅仅用于对static的理解,不喜勿喷
第一种方式:从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();
}
}
}
Java Static:作为修饰符, 可以用来修饰变量、方法、代码块(但绝对不能修饰类)。
1、修饰变量:
类的所有对象共同拥有的一个属性,也称为类变量。这类似于C语言中的全局变量。类变量在类加载的时候初始化,而且只被初始化一次。在程序中任何对象对静态变量做修改,其他对象看到的是修改后的值。因此类变量可以用作计数器。另外,Java Static变量可以用类名直接访问,而不必需要对象。
2、修饰方法:
类的所有对象共同拥有的一个功能,称为静态方法。静态方法也可以用类名直接访问,而不必需要对象。所以在静态方法里不能直接访问非静态变量和非静态方法,在Static方法里不能出现this或者super等关键字。
3、修饰Java代码块:
用static去修饰类里面的一个独立的代码块,称为静态代码块。静态代码块在类第一次被加载的时候执行,而且只执行一次。静态代码块没有名字,因此不能显式调用,而只有在类加载的时候由虚拟机来调用。它主要用来完成一些初始化操作。
4、说说类加载:
JVM在第一次使用一个类时,会到classpath所指定的路径里去找这个类所对应的字节码文件, 并读进JVM保存起来,这个过程称之为类加载。
可见,无论是变量,方法,还是代码块,只要用static修饰,就是在类被加载时就已经"准备好了",也就是可以被使用或者已经被执行。都可以脱离对象而执行。反之,如果没有static,则必须通过对象来访问。
引用地址:http://blog.csdn.net/sniperwang/article/details/4744753