使用intent时,在库模块中使用应用程序模块依赖项

时间:2014-09-29 18:00:18

标签: android android-intent android-gradle

我正在使用谷歌手机后端启动器https://cloud.google.com/cloud/samples/mbs/,一切正常,直到我想尝试从GCMintentService.java发送通知。如果我打电话

Intent resultIntent = new Intent(this, MyActivity.class);

我收到错误,即App模块中的MyActivity不存在。正如这里所解释的那样error: package does not exist android google mobile backend starter这是因为我不能依赖库模块中的应用程序模块。

所以我的问题是,如果我想在库模块中使用intent来调用应用程序模块中的活动,我该怎么做?我已经包含了一些代码,以防它对这个特定的例子有所帮助:

public void generateNotification(){
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.notification_icon)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");

    // The line below is the one causing the problem with RideListActivity which is in the application module
    Intent resultIntent = new Intent(this, RideListActivity.class);
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);

    int mNotificationId = 001;

    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    mNotifyMgr.notify(mNotificationId, mBuilder.build());
};

1 个答案:

答案 0 :(得分:1)

通常,您不会对库进行代码更改,您将在应用程序中按原样使用库,并实现其接口或调用它。上面的代码片段应该从您的模块运行,并且GCM库应该用作依赖项。

如果您已在模块(核心模块)中执行此操作,并且想要在第二个模块(应用程序模块)中调用活动,则需要声明并使用意图过滤器。 Please see this for a sample code snippet。在这种情况下,这两个模块(“apps”)都需要在设备上运行才能进行测试。

相关问题