【问题标题】:Notification.Builder does not create a notificationNotification.Builder 不创建通知
【发布时间】:2014-09-11 08:25:29
【问题描述】:

我正在尝试从编辑文本和广播接收器创建通知。在我的第一个活动中,用户应该输入一条消息并按下广播按钮。我想获取该字符串并从中创建一个通知并打开一个显示该消息的新活动。我正在我的广播接收器类中完成所有通知工作。 我在网上查看了示例和其他人的代码,但我不确定我做错了什么。应用程序加载得很好,广播按钮将广播发送到接收器并记录字符串,但从未创建通知。 感谢您的帮助。

发送广播消息的广播类:

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

}
}

创建通知的接收器类:

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 的不熟悉有何不同。 任何事情都有帮助 谢谢。

【问题讨论】:

  • 我不确定,但我认为问题可能在于,您在活动启动时直接取消了消息。尝试注释掉 notificationmanager.cancel(0);只是为了测试。
  • 我试过了,但它没有改变任何东西。 ViewNotification 类仍然没有到达。不过还是谢谢。
  • 但是您确定广播到达了吗?
  • 好的.....看到你的回答太晚了......

标签: java android notifications broadcastreceiver android-notifications


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2014-03-06
    相关资源
    最近更新 更多