qq_37436064 2017-02-02 04:17 采纳率: 0%
浏览 2259

读取.txt无法实时刷新

public class SecondActivity extends Activity {
private GifImageView shui;
String stringURL = "http://192.168.1.100:8080/MyUrlSample/msg.txt";
private GoogleApiClient client;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
shui = (GifImageView) findViewById(R.id.shui);
shui.setImageResource(R.drawable.shui);
final android.widget.MediaController mediaController = new android.widget.MediaController(this);
mediaController.setMediaPlayer((GifDrawable) shui.getDrawable());
int fv = Build.VERSION.SDK_INT;
if (fv > 13) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.penaltyLog()
.penaltyDeath()
.build());
}
super.onCreate(savedInstanceState);
setContentView(secondactivity);

    getStringURLResources();

    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

public void getStringURLResources() {
    try {
        URL myUrl = new URL(stringURL);
        URLConnection myConn = myUrl.openConnection();
        InputStream in = myConn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(in);
        ByteArrayOutputStream baf = new ByteArrayOutputStream(bis.available());
        int data = 0;
        while ((data = bis.read()) != -1) {
            baf.write((byte) data);
        }
        String msg = EncodingUtils.getString(baf.toByteArray(), "gbk");
        TextView et = (TextView) findViewById(R.id.et);
        et.setText(msg);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public class LongRunningService extends Service{
    @Override
    public IBinder onBind(Intent intent){
        return null;
    }
    @Override
    public int onStartCommand(Intent intent,int flags, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                updateShui();
            }
        }).start();
        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        long triggerAtTime = SystemClock.elapsedRealtime() + 3*1000;
        Intent i = new Intent(this, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,triggerAtTime,pi);
        return super.onStartCommand(intent, flags, startId);
    }

}

private void updateShui(){
String stringURL = "http://192.168.1.100:8080/MyUrlSample/msg.txt";
setContentView(R.layout.secondactivity);
try {
URL myUrl = new URL(stringURL);
URLConnection myConn = myUrl.openConnection();
InputStream in = myConn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream baf = new ByteArrayOutputStream(bis.available());
int data = 0;
while ((data = bis.read()) != -1) {
baf.write((byte) data);
}
String msg = EncodingUtils.getString(baf.toByteArray(), "gbk");
TextView et = (TextView) findViewById(R.id.et);
et.setText(msg);
} catch (Exception e) {
e.printStackTrace();
}
}

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent){
        Intent i = new Intent(context, LongRunningService.class);
        context.startService(i);
    }
}

}

  • 写回答

1条回答 默认 最新

  • -逍遥剑- 2017-02-14 11:05
    关注

    updateUI没有在UI线程中进行吧,应该会有ui卡的情况吧

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog