首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

在Handler中 如何重新启动 Activity?

2012-08-29 
在Handler中 怎么重新启动 Activity???????package com.androidimport com.android.FootBallaActivityim

在Handler中 怎么重新启动 Activity???????
package com.android;
import com.android.FootBallaActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.SyncStateContract.Constants;
import android.widget.Toast;

public class goalThread extends Handler {
Context context;
FootBallaActivity football;

public void getContent(Context context){
this.context=context;
}
public void handleMessage(Message msg) {
football=new FootBallaActivity();
// TODO Auto-generated method stub
switch (msg.what) {
  case 1:
  // We are now back in the UI thread
  AlertDialog.Builder normalDia=new AlertDialog.Builder(context);
  normalDia.setTitle("普通的对话框");
  normalDia.setMessage("你输了!再来一局?");
   
  normalDia.setPositiveButton("确定",new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
  // TODO Auto-generated method stub
  football.start();
  //此处调用FootBallaActivity的Start(),
  //other.startSucc();
  }
  } );
  normalDia.setNegativeButton("退出", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
  // TODO Auto-generated method stub
  // showClickMessage("取消");
  System.exit(0);
  }
  });
  normalDia.create().show();
  break;
  case 0:
  AlertDialog.Builder normalDia2=new AlertDialog.Builder(context);
  normalDia2.setTitle("小游戏");
  normalDia2.setMessage("你赢了!是否重来一局?");
  normalDia2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
  // TODO Auto-generated method stub
  // showClickMessage("确定");
  football.start();
  //other.startSucc();
 
 
  }


  });
  normalDia2.setNegativeButton("退出", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
  // TODO Auto-generated method stub
  // showClickMessage("取消");
  System.exit(0);
  }
  });
  normalDia2.create().show();
  break;
  }

}

}
/*******************/
football.start();

在FootBallaActivity中方法
  public void start(){
  Intent intent = getIntent();
  finish();
  startActivity(intent);

  }

[解决办法]
你可以这样做:

1、注册一个广播接收机,用于接收重启的广播;

2、在原Activity里面向这个广播接收机发送广播,发送后执行finish();

3、在广播接收机里面接收到广播后,用wait或者sleep方式延迟1秒左右再启动你这个Activity。

热点排行