2014-10-07 64 views
0

我跟着他们的教程一步一步,但我不断收到奇怪的错误。如何创建Android通知?

notifymanager没有将.notify()作为方法,并且任何带有notifymanager的代码行都必须包含括号,而不管它在代码中的何处。

我开始认为这是一个依赖性问题,请大家帮忙!

+0

你以下什么教程?我希望[官方通知指南](http://developer.android.com/guide/topics/ui/notifiers/notifications.html)? – ianhanniballake 2014-10-07 02:31:29

回答

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 2014-10-07 02:30:16