0

我試圖從編輯文本和廣播接收器創建通知。在我的第一個活動中,用戶應該輸入一條消息並按下廣播按鈕。我想獲取該字符串並從中創建通知並打開顯示該消息的新活動。我正在做廣播接收機課程中的所有通知工作。 我看了看周圍的例子和其他人的代碼,但我不知道什麼我沒有得到正確的。應用程序加載正常,廣播按鈕將廣播發送給接收方並記錄字符串,但通知從不創建。 感謝您的幫助。Notification.Builder不會創建通知

發送廣播消息

廣播類:

創建
public class BroadcastReceiverActivity extends Activity 
{ 
EditText et; 
Button btn1; 
public static String BString = "HappyHemingway"; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_broadcast_receiver); 
    et = (EditText)findViewById(R.id.et1); 
    btn1 = (Button)findViewById(R.id.btn1); 
    btn1.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      String message = et.getText().toString(); 
      send(message); 
     } 
    }); 
} 

/* 
* This function creates an intent and 
* sends a broadcast from the message 
* parameter passed in. 
*/ 
protected void send(String msg) 
{ 
    Log.i("msg", msg); 
    Intent i = new Intent(); 
    i.putExtra("message",msg); 
    i.setAction(BString); 
    sendBroadcast(i); 

} 
} 

Receiver類通知:

,其從未啓動

public class ViewNotification extends Activity 
{ 

String text; 
TextView txttext; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viewnotification); 


    NotificationManager notificationmanager; 
    notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    notificationmanager.cancel(0); 

    Intent i = getIntent(); 

    text = i.getStringExtra("message"); 
    txttext = (TextView) findViewById(R.id.text); 
    txttext.setText(text); 
    Log.i("made it", "made it made it made it"); 
} 

} 

清單

public class Receiver extends BroadcastReceiver 
{ 
// @SuppressLint("NewApi") 
@Override 
public void onReceive(Context context, Intent intent) 
{ 
    String action = intent.getAction(); 
    if(action!=null&&action.equals("HappyHemingway")) 
    { 

     String msg = intent.getStringExtra("message"); 
     Log.i("Received",msg); 

     Intent i = new Intent(context,ViewNotification.class); 
     i.putExtra("message",msg); 

     PendingIntent pi = PendingIntent.getActivity(context, 0, i, 
        PendingIntent.FLAG_UPDATE_CURRENT); 

     Notification.Builder builder = new Notification.Builder(context). 
       setSmallIcon(0).setAutoCancel(true).setTicker(msg). 
       setWhen(System.currentTimeMillis()).setContentTitle("New Notification!"). 
       setContentText(msg).setContentIntent(pi); 

     NotificationManager mgr = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification n = builder.build(); 
     mgr.notify(0, n); 
     Log.i("Received again",msg); 

    } 
} 
} 

通知觀察者類

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".BroadcastReceiverActivity" 
     android:label="@string/app_name" > 
      <action android:name="android.intent" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
    </activity> 
    <activity android:name=".ViewNotification"></activity> 
    <receiver android:name="Receiver"> 
      <intent-filter> 
        <action android:name="HappyHemingway"> 
        </action> 
      </intent-filter> 
</receiver> 

</application> 

</manifest> 

希望它只是一個簡單的錯誤,我俯瞰。這是我第一次使用Android Studio而不是Eclipse,但我沒有看到如何能夠比我不熟悉IDE有什麼區別。 任何有用的幫助 謝謝。

+0

我不確定,但我認爲問題可能在於,您在活動開始時直接取消該消息。嘗試註釋掉notificationmanager.cancel(0);只是爲了測試。 – Opiatefuchs 2014-09-11 09:26:17

+0

我試過,但它沒有改變任何東西。 ViewNotification類仍然沒有到達。不過謝謝。 – 2014-09-11 17:59:12

+0

但您確定廣播已到達? – Opiatefuchs 2014-09-12 06:28:13

回答

0

我不知道爲什麼我有setSmallIcon(0。) 當我將其更改爲setSmallIcon(R.drawable.ic_launcher)時,一切正常。