无法通过活性在方法中的参数 [英] unable to pass activity as a parameter in method

查看:99
本文介绍了无法通过活性在方法中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里的交易。​​

我创建了一个用户定义的类。它包含返回一个通知对象的方法。现在,我想这个方法是有点弹性。就像通过当用户点击通知栏上的通知,这将打开活动。这里的方法

I created a user defined class. It contains a method which returns a notification object. Now I want this method to be a little bit flexible. Like pass the activity which will open when the user clicks the notification in the notification bar. Here's the method

public Notification getUserNotificationObject(String status, String message, String tickerText, boolean isOngoingEvent){
    Notification notification = new Notification(R.drawable.image, tickerText, System.currentTimeMillis());

    long vibInterval =  (long) context.getResources().getInteger(R.integer.vibrateInterval);

    notification.vibrate = new long[] {vibInterval, vibInterval, vibInterval, vibInterval, vibInterval};

    Intent notifyIntent = new Intent(context, HomeScreen.class);
    CharSequence contentTitle = "Title";
    CharSequence contentText = status + "-" + message;
    notification.setLatestEventInfo(context, contentTitle, contentText, PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_CANCEL_CURRENT));

    notification.ledARGB = Color.argb(100, 0, 254, 0);
    notification.ledOnMS = 500;
    notification.ledOffMS = 500;        
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    if(isOngoingEvent){
         notification.flags |= Notification.FLAG_ONGOING_EVENT;
    }

    return notification;
}

我希望能够传递活动作为参数在此代替

I want to be able to pass the activity as a parameter in this instead of the

HomeScreen.class

上面使用在意图定义(给附加控制这个类(或其他开发者)的用户能够选择要打开的活动通知被点击时)。我试图用活动作为参数,这个方法之一,但每当我试图通过同时调用此方法,如活性2或Activity2.this另一个活动它给了我错误说:

used above in intent definition (for giving additional control to the user of this class (or other developers) to select which activity to open when notification is clicked). I tried using Activity as one of the parameters for this method, but whenever I tried passing another activity while calling this method like "Activity2" or "Activity2.this" it gives me error saying:

No enclosing instance of the type Activity2 is accessible in scope

有没有变通的这个或任何方式传递活动作为参数。或者我应该只是区分那些基于NotificationID。

Is there any work-around for this or any way to pass the activity as a parameter. Or should I just differentiate those based on NotificationID.

任何帮助在这方面或任何校正在code以上是值得欢迎的。 (背景是一个类级别的变量,所以不要担心。这片code是工作的罚款)。

Any help in this regard or any correction in the code above is welcome. ("context" is a class level variable so don't worry about that. This piece of code is working fine).

推荐答案

的类型 HomeScreen.class 。所以,你可以通过一个的实例来说明下一个活动。例如(格式化以提高可读性):

The type of HomeScreen.class is Class. So you could pass an instance of a Class to indicate the next activity. For example (formatted for readability):

public Notification getUserNotificationObject(
    String status,
    String message,
    String tickerText,
    boolean isOngoingEvent,
    Class nextActivityClass) {

和调用:

getUserNotificationObject(..., HomeScreen.class)

更灵活,不过,可能是通过一个意图给你的函数。这样一来,调用者可以创建意图在他们希望的方式,并允许他们更多的数据添加到意图,如果他们需要。创建一个新意图在你的函数不允许这种灵活性。

More flexible, though, might be to pass an Intent to your function. That way, the caller can create the Intent in the way they want, and would allow them to add additional data to the intent if they need to. Creating a new Intent inside your function does not permit that flexibility.

public Notification getUserNotificationObject(
    String status,
    String message,
    String tickerText,
    boolean isOngoingEvent,
    Intent nextIntent) {

和调用:

Intent intent = new Intent(context, HomeScreen.class);
getUserNotificationObject(..., intent)

这篇关于无法通过活性在方法中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆