jsjxsy 2011-10-09 16:57
浏览 508
已采纳

android service刷新 view 问题?

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]

  • 写回答

2条回答 默认 最新

  • 一口三个汉堡 2011-10-10 11:07
    关注

    public static Activity getActivityByName(String name)

    {

    for(Activity ac:allActivity)

    {

    if(ac.isFinishing())continue;
    if(ac.getClass().getName().indexOf(name)>=0)

    {

    return ac;

    }

    }

    return null;

    }

    加上这一句就可以了if(ac.isFinishing())continue;
    我个人认为可能是因为那个已经被关闭了,所以才不会显示.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办