2011-08-14 12 views
1

通知を表示し、毎回繰り返す必要があります。他のアクティビティでOnReceive()で通知を作成したいときにエラーが発生する

The method getSystemService(String) is undefined for the type OnAlarmReceiverThe constructor Intent(OnAlarmReceiver, Class<Acceuil>) is undefined 
The method getActivity(Context, int, Intent, int) in the type PendingIntent is not applicable for the arguments (OnAlarmReceiver, int, Intent, int) 

:私はこのエラーを持っている理由しかし、私は知らない

public class OnAlarmReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 


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

     int icon = R.drawable.icon; 
     CharSequence tickerText = "Votre vidange approche"; 
     long when = System.currentTimeMillis(); 

     Notification notification = new Notification(icon, tickerText, when); 


     CharSequence contentTitle = "Notification"; 
     CharSequence contentText = "Vérifier votre kilométrage"; 
     Intent notificationIntent = new Intent(this, Acceuil.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

     notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

     final int HELLO_ID = 1; 

     mNotificationManager.notify(HELLO_ID, notification); 
    } 

:私はそれを作る方法を読んで、私は、ブロードキャストレシーバーを拡張するクラスでそのコードを集約し私はこれをやらなければならないと思う:

public void notifs() { 
     AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
     Intent i=new Intent(context, OnAlarmReceiver.class); 
     PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0); 

     mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 1800000, pi); 
    } 

ここで問題は何ですか? ありがとうございます。

答えて

3

違いがある:あなたがこの上でそれを呼び出すようにしようとしている最初のようにアプリケーション・コンテキスト、上getSystemServiceを呼び出すため、2番目の1作品

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 

この時点では、getSystemServiceというメソッドを持たないBroadcastReceiverのサブクラスのインスタンスです。

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


PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

あなたはそれを期待しのようなこのオブジェクトの代わりに、Contextオブジェクトとしてカスタムクラスのインスタンスを渡そうとしているので、このラインはあなたに問題を与える:このDOを修正するには

。このDOを修正するには

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
+0

あなたが最高です。エラーはありません。うまくいけば、私は望む結果を得ます:\。 – androniennn

関連する問題