0

通知を設定しようとしていますが、ブロードキャストレシーバークラスです..電話が再起動したとき..

ここにコードがあります

public class OnBootReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  aCtx=context.getApplicationContext();
   String ns = aCtx.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);

 }
  }

「メソッド getSystemService(String) は、タイプ OnBootReceiver に対して未定義です」というエラーが表示されます。

誰でも助けてください:(

4

1 に答える 1

2

The getSystemService method is a member of the Context class. You seem to be trying to call it directly without referencing a Context object hence the message "The method getSystemService(String) is undefined for the type OnBootReceiver".

Changing your last line to

NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns);

should do the trick for you.

Perhaps your confusion stems from the fact within an Activity object you can simply call the getSystemService method and it works without referencing any object. This is because the Activity class itself is a subclass of Context. Calling getSystemService() without referencing any object works in this case because the object you are calling from is a Context object.

于 2011-12-13T09:38:11.847 に答える