Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通知。生成器不创建通知_Java_Android_Notifications_Broadcastreceiver_Android Notifications - Fatal编程技术网

Java 通知。生成器不创建通知

Java 通知。生成器不创建通知,java,android,notifications,broadcastreceiver,android-notifications,Java,Android,Notifications,Broadcastreceiver,Android Notifications,我正在尝试从edittext和广播接收器创建通知。在我的第一个活动中,用户应该输入一条消息并按下广播按钮。我想获取该字符串并从中创建一个通知,然后打开一个显示消息的新活动。我正在做广播接收器课上所有的通知工作。 我在网上查看了一些示例和其他人的代码,但我不确定我没有做对什么。应用程序加载得很好,广播按钮将广播发送给接收器并记录字符串,但从未创建通知。 谢谢你的帮助 发送广播消息的广播类: public class BroadcastReceiverActivity extends Activit

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

发送广播消息的广播类:

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

}
显示


希望这只是我忽略的一个简单错误。这是我第一次使用Android Studio而不是Eclipse,但我不认为这会比我不熟悉IDE有什么不同。 有什么帮助吗
谢谢。

我不知道为什么我要设置mallicon(0。)
当我把它改为setSmallIcon(R.drawable.ic_launcher)时,一切正常

我不确定,但我认为问题可能是,当活动开始时,您直接取消消息。尝试注释掉notificationmanager。取消(0);只是为了测试。我试过了,但没什么改变。类ViewNotification仍然无法到达。谢谢。但是你确定广播已经到达了吗?好的……看到你的答案太晚了。。。
<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>