2012-05-13 26 views
2

だからここで取り引きします。メソッド内のパラメータとしてアクティビティを渡すことができません

私はユーザー定義のクラスを作成しました。通知オブジェクトを返すメソッドが含まれています。今私はこの方法を少し柔軟にしたい。通知バーでユーザーが通知をクリックしたときに開くアクティビティを渡します。ここでは(このクラスのユーザーに追加の制御を与えるため

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; 
} 

は、私が意図定義で使用さ

HomeScreen.class 

の代わりにこの中にパラメータとして活動を渡すことができるようにしたい方法です(または他の開発者)が通知をクリックしたときに開くアクティビティを選択する)。私は、このメソッドのパラメータの1つとして活動を使用してみましたが、私は「Activity2」のように、このメソッドを呼び出すか、「Activity2.this」しながら、別のアクティビティを渡してみました時はいつでも、それは言って私にエラーを与える:

No enclosing instance of the type Activity2 is accessible in scope 

がいずれかがありますこれを回避するか、アクティビティをパラメータとして渡すことができます。あるいは、私はNotificationIDに基づいてそれらを区別する必要があります。

この点に関する助けや上記のコードの修正は大歓迎です。 ( "コンテキスト"はクラスレベルの変数なので、心配しないでください。このコードはうまくいきます)。

答えて

2

HomeScreen.classのタイプはClassです。したがって、次のアクティビティを示すためにClassのインスタンスを渡すことができます。例えば、(読みやすくするためにフォーマット):

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

として呼び出します。

getUserNotificationObject(..., HomeScreen.class) 

は、より柔軟な、しかし、あなたの関数にIntentを渡すかもしれません。こうすることで、発信者は希望の方法でIntentを作成し、必要に応じて追加のデータをインテントに追加することができます。関数内にnew Intentを作成しても、その柔軟性は許されません。

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

として呼び出します。

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

私もクラスでやってみましたが、同じ気持ちでした。しかし、インテントは素晴らしい考えです。 Gregに感謝します。回答が受け入れられました:D – drulabs

-1

をジャスト新しいYourActivity()様活性オブジェクト/インスタンスを作成します。

public static void Redirect(Context context,Activity page) { 

..... //code 

context.startActivity(new Intent(context,page.getClass())); 

((Activity) context).finish(); 
} 

とそれはActivityを渡す必要はありません

Redirect(Registration.this, new YourActivity()); 
0

として、このメソッドを使用します。あなたは簡単に、その後、以下のようActivityにキャストし、Contextを渡すことができます。

public class SomeClass { 
    public SomeClass(Context context){ 

     // using context as activity 
     Window win = ((Activity) context).getWindow(); 

     // your code 
    } 
} 

は、あなたがそれが役に立つことを願っ!

関連する問題