2014-10-07 2 views
0

Я последовал их учебному пособию шаг за шагом, но я все время получаю странные ошибки.Как создать уведомление для Android?

У notifymanager не существует .notify() как метод, и любая строка кода с notifymanager в нем должна иметь скобки независимо от того, где она находится внутри кода.

Я начинаю думать, что это проблема зависимости, пожалуйста, помогите!

+0

Что обучающая вы преследуете? Я хотел бы надеяться на [официальное руководство по уведомлениям] (http://developer.android.com/guide/topics/ui/notifiers/notifications.html)? – ianhanniballake

ответ

1

активность 1

import android.os.Bundle; 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 


public class NotificationAlert extends Activity { 


private static final int NOTIFY_ME_ID=1337; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.notification_alert); 

    /*********** Create notification ***********/ 

    final NotificationManager mgr= 
     (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification note=new Notification(R.drawable.stat_notify_chat, 
                "Android Example Status message!", 
                System.currentTimeMillis()); 

    // This pending intent will open after notification click 
    PendingIntent i=PendingIntent.getActivity(this, 0, 
              new Intent(this, NotifyMessage.class), 
              0); 

    note.setLatestEventInfo(this, "Android Example Notification Title", 
          "This is the android example notification message", i); 

    //After uncomment this line you will see number of notification arrived 
    //note.number=2; 
    mgr.notify(NOTIFY_ME_ID, note); 


} 
} 

активность 2

import android.app.Activity; 
    import android.os.Bundle; 
    import android.widget.TextView; 

public class NotifyMessage extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     TextView txt=new TextView(this); 

     txt.setText("Activity after click on notification"); 
     setContentView(txt); 
    } 
} 
+0

Возможно, это был бы более правильный код, если он был написан 3 года назад, до того, как [NotificationCompat] (http://developer.android.com/reference/android/support/v4/app/NotificationCompat.html) стал лучшим и только рекомендуемый способ создания уведомлений согласно [руководству по уведомлениям] (http://developer.android.com/guide/topics/ui/notifiers/notifications.html) – ianhanniballake

Смежные вопросы