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)时,一切正常。