跳转至

Android 使用Intent模拟点击Launcher启动

更新日期 2024-06-27
  • 2024-06-27 创建文档

使用Intent模拟点击Launcher启动

Intent需要:

  • setAction(Intent.ACTION_MAIN);
  • addCategory(Intent.CATEGORY_LAUNCHER);

在自己App内,已知具体的启动activity的情况下

1
2
3
4
5
6
Intent intent = new Intent(Intent.ACTION_MAIN);
// 这里模拟launcher的intent 否者从桌面上点击会启动新的页面 造成混乱
intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(getPackageName(), ChooseUseTypeActivity.class.getName()));

已知目标App的包名情况下,根据包名使用PackageManager寻找启动activity

public static Intent getAppOpenIntentByPackageName(Context context, String packageName) {
    // MainActivity完整名
    String mainAct = null;
    // 根据包名寻找MainActivity
    PackageManager pkgMag = context.getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED|Intent.FLAG_ACTIVITY_NEW_TASK);

    List<ResolveInfo> list = pkgMag.queryIntentActivities(intent,
            PackageManager.GET_ACTIVITIES);
    for (int i = 0; i < list.size(); i++) {
        ResolveInfo info = list.get(i);
        if (info.activityInfo.packageName.equals(packageName)) {
            mainAct = info.activityInfo.name;
            break;
        }
    }
    if (TextUtils.isEmpty(mainAct)) {
        return null;
    }
    intent.setComponent(new ComponentName(packageName, mainAct));
    return intent;
}

在Notification中的使用

假设某个Service弹出了通知Notification,点击通知要模拟launcher点击App。关键代码如下

public static final int INCOME_NOTIFY_ID = 1;

private void dealNotification(PushEntity pushEntity, String showBody) {
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent;
    PendingIntent pendingIntent;

    intent = new Intent(Intent.ACTION_MAIN);
    // 点击跳转到首页 并且表明要自动登录进去
    // 这里模拟launcher的intent 否者从桌面上点击会启动新的页面 造成混乱
    intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    intent.setComponent(new ComponentName(getPackageName(), ChooseUseTypeActivity.class.getName()));
    pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_rustfisher_logo)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_rustfisher_logo))
                    .setContentTitle(apsInfo.getAlert())
                    .setContentText("Click me back to app. See an.rustfisher.com")
                    .setAutoCancel(true)
                    .setTimeoutAfter(30000)
                    .setSound(defaultSoundUri)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setContentIntent(pendingIntent);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId,
                "RustFisher",
                NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
    }
    notificationBuilder.setTimeoutAfter(30000);
    notificationManager.notify(INCOME_NOTIFY_ID, notificationBuilder.build());
}

  • Intent要setAction(Intent.ACTION_MAIN)addCategory(Intent.CATEGORY_LAUNCHER)
  • Notification需要用到PendingIntent
  • SDK_INT大于等于26(Build.VERSION_CODES.O)时需要使用NotificationChannel
  • 手机设置中,需要打开对应NotificationChannel的悬浮通知权限

本案例在FCM service中使用,vivo手机测试ok

本站说明

一起在知识的海洋里呛水吧。广告内容与本站无关。如果喜欢本站内容,欢迎投喂作者,谢谢支持服务器。如有疑问和建议,欢迎在下方评论~

📖AndroidTutorial 📚AndroidTutorial 🙋反馈问题 🔥最近更新 🍪投喂作者

Ads