2011-11-09 18 views
0

私は基本的にちょっと固まってしまいました。通知バーに通知を表示するクラスのメソッドがあります。私はそれを静的にしようとしましたが、私が静的にすると、いくつかの関数は動作しません。別のクラスからアクセスする方法

私はx.classで次のような機能を持っていますが、どうすればy.classからアクセスできますか?なぜなら私は静的で、そしてオブジェクトを使ってみましたが、すべて失敗しました。

void notify(String i) { 

     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 


     int icon = R.drawable.icon;  // icon from resources 
     CharSequence tickerText = "gogu la telefon";    // ticker-text 
     long when = System.currentTimeMillis();   // notification time 
     Context context = getApplicationContext();  // application Context 
     CharSequence contentTitle = "My notification"; // message title 
     CharSequence contentText = "Hello World!";  // message text 



     Intent notificationIntent = new Intent(this, MilkyWaySearcherActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

     // the next two lines initialize the Notification, using the configurations above 
     Notification notification = new Notification(icon, tickerText, when); 
     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
     notification.ledARGB = 0xff00ff00; 
     notification.ledOnMS = 300; 
     notification.ledOffMS = 1000; 
     notification.flags |= Notification.FLAG_SHOW_LIGHTS; 

     mNotificationManager.notify(BIND_AUTO_CREATE, notification); 
    } 

答えて

2

インスタンスを作成するか、メソッドを静的にする必要があります。

インスタンスが存在しない限り、静的メソッドはインスタンスメソッドにアクセスできません。

参照するインスタンスがないため、静的メソッドではthisキーワードを使用できません。

この場合、thisを使用する場所を交換するだけで十分です。

+0

getApplicationContext();私もそれを使用することはできません...静的にする場合 – user1015311

+0

あなたが必要とするインスタンス化された変数は、メソッドのパラメータを渡すことができます。 'Context'が必要な場合は、メソッドを' void notify(String i、Context context) 'に変更してください。 –

+0

それはパラメータで働いていました...私はそれについて考えることができなかったので、愚かです。 – user1015311

関連する問題