仿QQ修改密码后弹出对话框(Jpush推送下实现)

在Application中写如下方法:
protected static boolean isTopActivity(Activity activity){
        String packageName = "xxxxx";
        ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo>  tasksInfo = activityManager.getRunningTasks(1);  
        if(tasksInfo.size() > 0){  
            System.out.println("---------------包名-----------"+tasksInfo.get(0).topActivity.getPackageName());
            //应用程序位于堆栈的顶层  
            if(packageName.equals(tasksInfo.get(0).topActivity.getPackageName())){  
                return true;  
            }  
        }  
        return false;
    }
需要添加android.permission.GET_TASKS权限

然后在Jpush的Reciever中的

 if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");

int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            String messagetype = "";
            try {
                JSONObject extrasJson = new JSONObject(extras);
                messagetype = extrasJson.getString("messagetype");
                Log.i("messagetype------", messagetype);
            } catch (Exception e) {
            }
            //在前台运行执行它
            if (MyApplication.getIntance().isTopActivity()){

                if ("1".equals(messagetype)) {
                    sp.edit().putString("userPassword_Account", "")
                            .putBoolean("autoLogin", false).commit();
                    MyDialog dialog = new MyDialog(MyApplication.getIntance(), R.style.ForwardDialog);//自定义Dialog布局
                    dialog.setCanceledOnTouchOutside(false);
                    dialog.getWindow()
                            .setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

                    dialog.show();
                }
            }else {
                if ("1".equals(messagetype)){
                    MyApplication.getIntance().exit();
                }
            }
3.自定义Dialog

public class MyDialog extends Dialog {
    private Button leftTextView, rightTextView;
    private Context context;

    public MyDialog(Context context) {
        super(context);
    }

    public MyDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog);
        leftTextView = (Button) findViewById(R.id.btn_deletecustomer);
        rightTextView = (Button) findViewById(R.id.btn_cancel);
        leftTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                Intent loginintent = new Intent(MyApplication.getIntance(), LoginActivity.class);
                loginintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                MyApplication.getIntance().startActivity(loginintent);
                MyApplication.getIntance().exit();
            }
        });
        rightTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                MyApplication.getIntance().exit();
                System.exit(0);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && KeyEvent.KEYCODE_BACK == keyCode){
             return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值