当前位置: 首页 > 知识库问答 >
问题:

Android Java在前台服务运行时从应用项目中的子模块获取主要活动

燕意蕴
2023-03-14

我正在写一个音频播放器类型的应用与离子和电容,一切都很好。

设置:

  • 我的应用程序正在Android上播放音频。
  • 我已经在我的插件中添加了前台服务(如果你离开应用程序,Android需要播放音频,这样你就可以显示启动/停止的通知)。
  • 这些都很好。

问题:

    不起作用的是从项目中的模块 capacitor-plugin-remote-audio.myforegroundservice中从 app访问 mainactivity。这可能吗?

图文并茂的截图来展示问题:

相关的代码块

    /* Used to build and start foreground service. */
    public void startForegroundService()
    {
        Log.d(TAG, "Start foreground service.");

        // Create notification default intent to open the MainActivity from Capacitor app when tapped.
        Intent intent = new Intent(this, "What goes here to get the MainActivity");
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainAppIntent, 0);

        createNotificationChannel();

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setOngoing(true)
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .setShowWhen(false)
            .setSmallIcon(android.R.drawable.ic_media_play) // TODO: fix this to use the app icon.
            .setContentTitle("Content Title");

        if (mainAppPendingIntent != null) {
            builder.setContentIntent(mainAppPendingIntent);
        }

        addPlayAndPauseButtons(builder);

        startForeground(1, builder.build());
    }

共有2个答案

充普松
2023-03-14

您正在尝试从另一个模块打开一个activity。检查这个答案,就可以解决你的问题。

乐正宏深
2023-03-14

启动服务时:

添加额外的包名(可能不硬编码,但随便):

// Start the foreground service + show required notification.
String packageName = getActivity().getPackageName();
Intent intent = new Intent(getContext(), MyForegroundService.class);
intent.putExtra(MyForegroundService.EXTRA_Top_Level_Package_Name, packageName);
intent.setAction(MyForegroundService.ACTION_START);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    getContext().startForegroundService(intent);
} else {
    getContext().startService(intent);
}

在服务中:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if(intent != null)
    {
        String action = intent.getAction();

        switch (action)
        {
            case ACTION_START:
                String packageName = intent.getExtras().get(EXTRA_Top_Level_Package_Name).toString();
                startForegroundService(packageName);
                break;
            case ACTION_STOP:
                stopForegroundService();
                break;
        }
    }
    return super.onStartCommand(intent, flags, startId);
}

启动方法

/* Used to build and start foreground service. */
public void startForegroundService(String packageName)
{
    Log.d(TAG, "Start foreground service.");

    // Create notification default intent to open the MainActivity from Capacitor app when tapped.
    Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    createNotificationChannel();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
        .setOngoing(true)
        .setPriority(NotificationManager.IMPORTANCE_MIN)
        .setCategory(Notification.CATEGORY_SERVICE)
        .setShowWhen(false)
        .setSmallIcon(android.R.drawable.ic_media_play) // TODO: fix this to use the app icon.
        .setContentTitle("Content Title")
        .setContentIntent(pendingIntent);

    addPlayAndPauseButtons(builder);

    startForeground(1, builder.build());
}
 类似资料:
  • 我有一个多模块java项目,我想在其中创建jar(bin、source、javadoc),但我很难从子项目配置中的子项目中获取变量。 这是我在根项目中的build.gradle文件: 这就是建筑。gradle文件子项目之一: 当我想运行构建脚本时,我得到了以下异常: 无法获取类型为org的对象的未知属性“mainClassName”。格雷德尔。应用程序编程接口。JAVA档案内部的默认舱单。 在子流

  • 我面临以下问题。我有一个多模块的Gradle项目。一个模块是我的根项目,第二个模块是集成测试。 为了让我的集成测试运行,复制任务需要首先运行,以便将一些资产移动到项目根目录上的文件夹中。 我已经在我的根项目上定义了这样一个任务,当我试图调用它时,它什么也不做(我尝试了几种不同的调用方式)。 由于失败,我继续在子项目本身上创建了以下任务: 这是我的另一个任务。我的目标是让它将根项目类以及在下生成的j

  • 我有一个这样的多项目构建 现在,在项目中,我已经为所有子项目声明了一个任务,如下所示: 当我运行时,我得到的输出类似于以下内容(其他内容): 现在,我尝试像这样为简单项目运行该任务 我得到的输出是: 失败:构建失败,但有一个异常。 > 哪里出错了:在项目中找不到任务你好:例子:简单项目。一些候选人是:帮助。 尝试:运行gradle任务以获取可用任务的列表。使用--stack跟踪选项来获取堆栈跟踪。

  • 航班类别:package com.rahul.flightreservation.entities; 表说明:ID航班号运营航空公司出发城市到达城市日期出发估计日期出发时间 错误:Hibernate:from Fligher where departurecity=?和到达=?和DateofDeparture=?2020-06-28 20:32:13.138警告5744---[nio-8083-e

  • 我正在IntelliJ IDEA终极版2020.2中制作一个maven项目。这是一个多模块项目。当我从其中一个模块运行一个类时,我会收到错误,但如果我从打包的jar运行,它会运行良好。这是应用程序的结构: 这是主要的pom.xml: pom。DatabaseTier模块中的xml: pom。报告模块中的xml: 这是我得到的错误: 初始化启动层java时出错。lang.LayerInstation

  • 我遇到了一个需求,但我无法获得正确的实施方式,因此需要您的帮助。 我想做什么我想根据收到的通知执行以下操作: 当应用程序处于打开状态且处于前台(即用户可见)且我收到通知时,我只需显示一个弹出窗口即可开始我的活动B 我做了什么?我已经达到了第1点和第2点。我想达到第三点。我试过下面的方法 然而,在这两种情况下,点#2和#3都返回true,所以我不能仅区分#3。 我也尝试了下面的每一个活动, 但是,它