android service刷新view第一次可以显示内容,按back键后才进入就不显示内容了但是同样刷新了控件!
BackKeyTestActivity.java
[code="java"]
package com.lenovo;
import com.lenovo.dao.CommonInterface;
import com.lenovo.service.MainService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BackKeyTestActivity extends Activity implements CommonInterface{
/** Called when the activity is first created. */
private Button bt1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt1 = (Button)findViewById(R.id.bt_start_another_activity);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(BackKeyTestActivity.this, AnotherActivity.class);
startActivity(intent);
MainService.Another_Start_Run=1;
intent.setClass(BackKeyTestActivity.this, MainService.class);
startService(intent);
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
Log.e("BackKeyTestActivity", "onResume()");
// Intent intent = new Intent();
// intent.setClass(BackKeyTestActivity.this, MainService.class);
// startService(intent);
super.onResume();
}
@Override
public void refresh(Object... param) {
// TODO Auto-generated method stub
}
}
[/code]
跳转到另一个AnotherActivity
[code="java"]
package com.lenovo;
import com.lenovo.dao.CommonInterface;
import com.lenovo.service.MainService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class AnotherActivity extends Activity implements CommonInterface{
private TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.another);
tv1 = (TextView)findViewById(R.id.tv_current_activity);
MainService.allActivity.add(this);
Log.e("Another Activity","onCreate()");
}
private void refresh(){
tv1.setText("这是另一个activity");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
Log.e("Another Activity","onDestroy()");
Intent intent = new Intent();
intent.setClass(AnotherActivity.this,MainService.class );
stopService(intent);
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
Log.e("Another Activity","onPause()");
super.onPause();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
Log.e("Another Activity","onRestart()");
super.onRestart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
Log.e("Another Activity","onResume()");
super.onResume();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
Log.e("Another Activity","onStart()");
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
Log.e("Another Activity","onStop()");
super.onStop();
}
@Override
public void refresh(Object... param) {
// TODO Auto-generated method stub
Log.e("Another Activity","refresh");
String content = (String)param[0];
tv1.setText(content);
MainService.Another_Start_Run=0;
}
}
[/code]
后台运行的Service
[code="java"]
package com.lenovo.service;
import java.util.ArrayList;
import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import com.lenovo.dao.CommonInterface;
public class MainService extends Service implements Runnable{
private boolean flag;
public static ArrayList<Activity> allActivity=new ArrayList<Activity>();//保存所有Activity
public static int Another_Start_Run=0;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public static Activity getActivityByName(String name)
{
for(Activity ac:allActivity)
{
if(ac.getClass().getName().indexOf(name)>=0)
{
return ac;
}
}
return null;
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
CommonInterface ia4=(CommonInterface)MainService.getActivityByName("AnotherActivity");
ia4.refresh(msg.obj);
super.handleMessage(msg);
}
};
@Override
public void run() {
// TODO Auto-generated method stub
while(flag){
if(Another_Start_Run == 1){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message msg = handler.obtainMessage();
msg.obj="开启另一个Activity";
handler.sendMessage(msg);
}
}
}
public void stopService(){
flag=false;
stopSelf();
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
Thread t = new Thread(this);
flag=true;
t.start();
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
flag=false;
super.onDestroy();
}
}
[/code]
使用的接口
[code="java"]
package com.lenovo.dao;
public interface CommonInterface {
public void refresh(Object ...param);
}
[/code]
AndroidManifest.xml
[code="xml"]
<?xml version="1.0" encoding="utf-8"?>
package="com.lenovo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BackKeyTestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AnotherActivity"></activity>
<service android:name="com.lenovo.service.MainService"></service>
</application>
[/code]