@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
PackageManager pm = getPackageManager();
ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), 0);
ActivityInfo ai = homeInfo.activityInfo;
Intent startIntent = new Intent(Intent.ACTION_MAIN);
startIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startIntent.setComponent(new ComponentName(ai.packageName, ai.name));
startActivitySafely(startIntent);
return true;
}
return super.onKeyDown(keyCode, event);
}


private void startActivitySafely(Intent intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "没正常启动", Toast.LENGTH_SHORT).show();
} catch (SecurityException e) {
Toast.makeText(this, "存在安全隐患",
Toast.LENGTH_SHORT).show();
Log.e(MYLOG,"Launcher does not have the permission to launch "
+ intent
+ ". Make sure to create a MAIN intent-filter for the corresponding activity "
+ "or use the exported attribute for this activity.",
e);
}
}

下面是在通知栏开启一个已经启动的Activity

public void createNotification() {
//int icon = android.R.drawable.ic_menu_revert;
int icon = R.drawable.icon;
Notification notification = new Notification(icon, "", notificationId);
notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;
notification.setLatestEventInfo(context, "", "", receiveIntent());
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.app_notification_layout);
notification.contentView = contentView;
notfionMgr.notify(notificationId, notification);
}

private PendingIntent receiveIntent() {
Intent appIntent = new Intent(Intent.ACTION_MAIN);
appIntent.addCategory(Intent.CATEGORY_LAUNCHER);
appIntent.setComponent(new ComponentName(context.getPackageName(),
context.getPackageName() + "." + localClassName));
appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
appIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, notificationId, appIntent, PendingIntent.FLAG_UPDATE_CURRENT);

//Intent intent = new Intent(context, HomeActivity.class);
////intent.putExtra(NONE_START_APP, NONE);
//intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//PendingIntent contentIntent = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return contentIntent;
}



更多相关文章

  1. Android实现桌面图标通知数量角标
  2. 如何使用java RESTful Web服务将通知推送到Android应用程序
  3. android通知适用于循环中的一个对象[重复]
  4. Android消息推送接收后,通知栏的显示
  5. 添加/修改子项的Firebase通知

随机推荐

  1. 技术解答面向对象的初步认识(C++ 类)
  2. C#调用的三维地球控件,看三维地球构建过程
  3. 正则表达式模式匹配字符串基础知识_正则
  4. ASP.NET全栈开发之在MVC中使用服务端验证
  5. .net WINFORM的GDI双缓冲的实现方法_实用
  6. C++11多线程编程基础入门
  7. .Net Core配置与自动更新的实现方法_实用
  8. C#中.NET框架的简介
  9. Razor TagHelper实现Markdown转HTML的方
  10. C#开发之微信小程序发送模板消息功能