【问题标题】:Get Activity.Class from service in Android从 Android 中的服务获取 Activity.Class
【发布时间】:2017-02-28 21:32:09
【问题描述】:

我正在编写使用 Android 原生插件的 Unity 移动应用程序。该插件创建后台定位服务,并在用户接近地图上的特定点时通知用户(顺便说一句,这些点是建筑纪念碑)。

所以Service会发送推送通知,当用户点击这个通知时,应用应该从之前暂停的地方打开。

为了创建这个通知,我编写了下面的代码:

Intent intent = new Intent(this, UnityPlayerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

Notification n = new Notification.Builder(this)
        .setContentTitle(title)
        .setContentText(text)
        .setSmallIcon(R.drawable.app_icon)
        .setDefaults(Notification.DEFAULT_ALL)
        .setPriority(Notification.PRIORITY_HIGH)
        .setContentIntent(pIntent)
        .setAutoCancel(true).build();

mNotificationManager.notify(notificationID, n);

诀窍在于 Unity 在应用生命周期中使用一个 Activity (UnityPlayerActivity)。但是我必须使用我的服务构建独立的 .jar 文件,该文件不能使用 UnityPlayerActivity 类,因为它不知道这一点。

我只能在运行时获取当前的Activity 实例,并在启动服务时使用它。

问题:在创建 Intent 时如何从 Service 中获取 Activity.class

感谢您的回复。

UPD: 我无法将我的服务放入 Intent 构造函数中。 Intent 构造函数中的第二个参数必须是Activity.Class,当用户单击通知时应该打开它。如果我把Service.Class 放在那里,什么都不会打开。

所以我需要将UnityPlayerActivity 实例作为第二个参数,我只能在运行时获取它,但不知道如何将其导入服务。

【问题讨论】:

  • Service extends Context。为什么需要 Activity?
  • 所以你有new Intent(this, this.getClass());
  • @cricket_007 哦,对不起,这是错误的答案。 Intent 构造函数中的第二个参数必须是Activity.Class,当用户单击通知时应该打开它。如果我把Service.Class 放在那里,什么都不会打开。
  • @cricket_007 所以我需要将UnityPlayerActivity 实例作为第二个参数,我只能在运行时获得,但不知道如何将其导入服务。
  • Class.forName("package.name.UnityPlayerActivity")?

标签: java android android-activity unity3d android-service


【解决方案1】:

我使用意图putExtra 方法实现了这样的事情。

团结方面:

public class GPSServiceAndroidTest : MonoBehaviour {

    private AndroidJavaObject activity = null;
    private AndroidJavaObject launcher = null;

    void Start () {

        using(AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")){
            activity = activityClass.GetStatic<AndroidJavaObject>("currentActivity");
        }

        using(AndroidJavaClass pluginClass = new AndroidJavaClass("com.softserve.gpstest.Launcher")){
            if(pluginClass!=null){
                launcher = pluginClass.CallStatic<AndroidJavaObject>("instance");
                launcher.Call("startTustanService", activity);
            }
        }
    }
}

Launcher类:

public class Launcher {

    private static Launcher instance;

    private Launcher() {
        Launcher.instance = this;
    }

    public static Launcher instance() {
        if(instance == null) {
            instance = new Launcher();
        }
        return instance;
    }

    public void startTustanService(Activity currentActivity){
        Intent notificationIntent = new Intent(currentActivity, currentActivity.getClass());
        startServiceIntent.putExtra("notificationIntent", notificationIntent);

        currentActivity.startService(new Intent(currentActivity, TestService.class));
    }
}

然后在Service的onStartCommand方法中得到这个意图:

mNotificationIntent = intent.getParcelableExtra("notificationIntent");

【讨论】:

  • 我的意思是public class定义
  • @cricket_007 现在正在处理此代码的 Unity 端表示,完成后我会更新我的答案并通知您
  • @cricket_007 扩展了我的答案。我在 Unity 端获得 Activity 实例,然后在我的插件中启动服务时使用它。
猜你喜欢
  • 2012-08-29
  • 1970-01-01
  • 2018-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多