AlarmManager 闪退之后唤醒应用


public class CrashHandler implements UncaughtExceptionHandler {

    /**
     *  初始化方法
     */
	public static void init() {
		Thread.setDefaultUncaughtExceptionHandler(new CrashHandler());
	}

	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		ex.printStackTrace();

		Activity activity = Environment.activity();
        //获取activity对象,可以通过基类Activity的静态方法获取
		if (activity != null && !(activity instanceof SplashActivity)) {
			
				//闪退打点
                // XXX
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			restartApp(activity);
		}
	}

	public static void restartApp(Activity activity) {
		if (activity == null) {
			return;
		}

		Intent intent = new Intent(activity.getApplicationContext(), SplashActivity.class);
		PendingIntent restartIntent = PendingIntent.getActivity(
				activity.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);

		AlarmManager mgr = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
		mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent);
        //杀死老线程
		android.os.Process.killProcess(android.os.Process.myPid());
	}

}

AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent。简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为我们广播一个我们设定的Intent,通常我们使用 PendingIntent,PendingIntent可以理解为Intent的封装包,简单的说就是在Intent上在加个指定的动作。在使用Intent的时候,我们还需要在执行startActivity、startService或sendBroadcast才能使Intent有用。而PendingIntent的话就是将这个动作包含在内了。

AlarmManager常用于定时闹钟,一下是一个例子

AlarmManager.RTC_WAKEUP休眠时会运行,如果是AlarmManager.RTC,在休眠时不会运行

//创建Intent对象,action为ELITOR_CLOCK,附加信息为字符串“你该打酱油了”  
Intent intent = new Intent("ELITOR_CLOCK");  
intent.putExtra("msg","你该打酱油了");    
  
//定义一个PendingIntent对象,PendingIntent.getBroadcast包含了sendBroadcast的动作。  
//也就是发送了action 为"ELITOR_CLOCK"的intent   
PendingIntent pi = PendingIntent.getBroadcast(this,0,intent,0);    
  
//AlarmManager对象,注意这里并不是new一个对象,Alarmmanager为系统级服务  
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);    
  
//设置闹钟从当前时间开始,每隔5s执行一次PendingIntent对象pi,注意第一个参数与第二个参数的关系  
// 5秒后通过PendingIntent pi对象发送广播  
am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),5*1000,pi);  

在Manifest.xml中注册广播接收器:

<receiver android:name=".MyReceiver">  
        <intent-filter>  
            <action android:name="ELITOR_CLOCK" />  
        </intent-filter>  
</receiver>  

重写onReceive()函数。

public class MyReceiver extends BroadcastReceiver  
{  
  
    @Override  
    public void onReceive(Context context, Intent intent)  
    {  
        // TODO Auto-generated method stub  
        Log.d("MyTag", "onclock......................");  
        String msg = intent.getStringExtra("msg");  
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();  
    }  
  
}  

 

转载于:https://my.oschina.net/carbenson/blog/792823

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值