Notification.Builder不会创建通知

时间:2014-09-11 08:25:30

标签: java android notifications broadcastreceiver android-notifications

我试图从edittext和广播接收器创建通知。在我的第一个Activity中,用户应该输入一条消息并按下广播按钮。我想获取该字符串并从中创建通知并打开一个显示该消息的新活动。我正在广播接收器类中进行所有通知工作。 我已经查看了示例和其他人的代码,但我不确定我不正确。应用程序加载得很好,广播按钮将广播发送到接收器并记录字符串但从未创建通知。 谢谢你的帮助。

发送广播消息的广播类:

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 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);

    }
 }
 }

从未启动的通知查看器类

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");
}

}

清单     

<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的不熟悉有什么不同。 一切都有帮助 感谢。

1 个答案:

答案 0 :(得分:0)

我不知道为什么我有setSmallIcon(0。) 当我将其更改为setSmallIcon(R.drawable.ic_launcher)时,一切正常。