android中在广播中弹出pop框的局限性

  广播的接收处理只有10s,所以一般不推荐在onreceive中弹出对话框,最多是通知或者吐司。

   一开始在广播中弹出一个对话框,是在设置进程中的,发现只要把设置进程杀死,这个框会自动消失的,用户无法进行操作,所以直接上代码:


在onreceive中起一个activity:

                                  Intent intentactivity = new Intent();
 intentactivity.setClass(context,WifiRemindActivity.class);
 intentactivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(intentactivity);


在manifest下将这个activity设置成dialog类型的:

<activity android:name="com.android.settings.wifi.WifiRemindActivity"
                  android:theme="@android:style/Theme.Dialog" 
                  android:taskAffinity=""
                  android:excludeFromRecents="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
             <meta-data android:name="hwc-theme"
                android:value="androidhwext:style/Theme.Emui.Dialog.Alert"/>//这个是配置相应的主题可以修改
        </activity>


接下来在activity中的处理:

public class WifiRemindActivity extends Activity {


    private static final String TAG = "WifiRemindActivity";
    private  AlertDialog mAlertDialog;
    private static Timer time = null;
    private static final String action = "com.android.telephony.USER_ACTION";
    private TelephonyManager telephonyMgr = null;
    private CheckBox mCheckbox;
    private static final String WIFI_STATE_CHANGE = "android.net.wifi.STATE_CHANGE";
    private IntentFilter mIntentFilter;
    private WifiInfo   wifiInfo;
    private WifiManager mwifiManager;
    private WifiStateReceiver  myreceiver;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(telephonyMgr  == null) {
            telephonyMgr = TelephonyManager.from(this);
        }
        showRemindAlert();//show出 对话框
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(WIFI_STATE_CHANGE);
        myreceiver = new WifiStateReceiver();
        registerReceiver(myreceiver, mIntentFilter);
    }
    private Handler hand =new Handler() {
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            if(msg.what ==  0x111) {
                if(mAlertDialog !=null)
                   mAlertDialog.dismiss();
                   WifiRemindActivity.this.finish();
Log.d("wanghui", "timeover");
            }
        }
    };


    private TimerTask task = new TimerTask(){
        public void run() {
            Message msg = new Message();
            msg.what =  0x111;
            hand.sendMessage( msg);
        }
    };


    private void showRemindAlert() {
        int themeID = getResources().getIdentifier("androidhwext:style/Theme.Emui.Dialog.Alert",null,null); 
        AlertDialog.Builder builder = new  AlertDialog.Builder(this,themeID);
        builder.setTitle(R.string.wifi_is_disconnect);                
        builder.setPositiveButton(R.string.open_confirm,new OpenDataConnectOKListener());
        builder.setNegativeButton(R.string.cancle_confirm, new CloseDataConnectOKListener());
        mAlertDialog = builder.create();
        final  View  layout = mAlertDialog.getLayoutInflater().inflate(R.layout.define_view_dialog,null);
        mCheckbox = (CheckBox) layout.findViewById(R.id.closeReminder);
        mAlertDialog.setView(layout);
        mAlertDialog.setCanceledOnTouchOutside(false);
        mAlertDialog.setCancelable(false);
        mAlertDialog.getWindow().setType(
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        mAlertDialog.getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        mAlertDialog.show();
        if(time != null){
            time.cancel();
        }
        time = new Timer();//定时让这个弹出框消失
        //add by wanghui for al812 HQ01398660 5min later
        time.schedule(task,5*60*1000);
    }


    private class OpenDataConnectOKListener implements OnClickListener {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {  
            Log.v(TAG, "TAG ok");
            telephonyMgr.setDataEnabled(true);
            if (mCheckbox.isChecked()) {//用户点击不在提示走的流程
            Settings.System.putString(getContentResolver(),"switch_mode_key","3");
            Intent intent = new Intent(action);
            WifiRemindActivity.this.sendBroadcast(intent);
            WifiRemindActivity.this.finish();
            Settings.System.putString(getContentResolver(),"flag_state","1");
            Log.v("TAG", "save="+mCheckbox.isChecked());
        } else{
              //modify by wanghui for al812 activity not finish
              WifiRemindActivity.this.finish(); 
 }
      }
    }
    private class CloseDataConnectOKListener implements OnClickListener {
        @Override
        public void onClick(DialogInterface dialog, int whichButton) {  
            Log.v(TAG, "TAG cancle");
            WifiRemindActivity.this.finish();
      }
    }


    class WifiStateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); 
            Log.d("wanghui: " , action);
               if(action.equals(WIFI_STATE_CHANGE)){
        if(info.getState().equals(NetworkInfo.State.CONNECTED)&&(mAlertDialog!=null)) {
                     mAlertDialog.dismiss();
                     WifiRemindActivity.this.finish();
    Log.d("wanghui", "reconnect");
                 }                    
            }
        }
    }
    
    @Override
    protected void onDestroy(){
super.onDestroy();
unregisterReceiver(myreceiver);
}
}

//布局文件define_view_dialog就没上传了只有一个textview和checkbox


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值