android通知应用程序工作不正常

android通知应用程序工作不正常,android,android-layout,android-intent,android-activity,Android,Android Layout,Android Intent,Android Activity,您好,最近我启动了android应用程序开发人员。所以你可以说我是一个初学者。我正在尝试开发一个基本的通知应用程序。此应用程序现在能够显示通知,但单击虚拟设备顶部的通知图标时,什么也没有发生。问题是它应该启动一个名为NotificationView的新活动,但它无法启动该活动 MainActivity.java package com.example.notification1; import android.app.Notification; import android.app.N

您好,最近我启动了android应用程序开发人员。所以你可以说我是一个初学者。我正在尝试开发一个基本的通知应用程序。此应用程序现在能够显示通知,但单击虚拟设备顶部的通知图标时,什么也没有发生。问题是它应该启动一个名为NotificationView的新活动,但它无法启动该活动

MainActivity.java

    package com.example.notification1;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {
   Button b1;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      b1=(Button)findViewById(R.id.button);
      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Notify("You've received new message","");
         }
      });
   }
   private void Notify(String notificationTitle, String notificationMessage){
      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
      @SuppressWarnings("deprecation")

      Notification notification = new Notification(R.drawable.ic_launcher,"New Message", System.currentTimeMillis());
      Intent notificationIntent = new Intent(this,NotificationView.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);

      notification.setLatestEventInfo(MainActivity.this, notificationTitle,notificationMessage, pendingIntent);
      notificationManager.notify(9999, notification);
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.

      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}
NotificationView.java

   package com.example.notification1;

import android.os.Bundle;
import android.app.Activity;

public class NotificationView extends Activity{
   @Override
   public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.notification);
   }
}
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notification1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NotificationView"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

不要使用静态通知ID。这不是个好主意

notificationManager.notify(9999, notification);
创建如下内容,例如:

int notificationId = System.currentTimeMillis()/1000;
notificationManager.notify(notificationId, notification);
这对我总是有用的。有关PendingEvent通知的更多信息


让我知道它是否适合您;)

尝试使用NotificationCompat,您将在不同的设备上获得更好的支持,我写道:

这是您的一个示例。并确保您已经在应用程序模块的
build.gradle
中编译了
com.android.support:appcompat-v7

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Intent intentNotif = new Intent(this, NotificationView.class);
intentNotif.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intentNotif, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(true)
                .setOngoing(false)
                .setContentIntent(pendIntent)
                .setContentTitle("Title here")
                .setContentText("Notification content!")
                .setTicker("This is ticker!");

int id = 1234;
mNotificationManager.notify(id, mBuilder.build());

我运行了您的代码,可以启动
NotificationView
,而无需更改代码中的任何内容

我想您可能没有在
AndroidManifest.xml
文件中声明
NotificationView
。所有活动都必须在
AndroidManifest.xml
中声明。您的
AndroidManifest.xml
必须如下所示:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".NotificationView"
        android:label="@string/app_name" />
</application>


main活动
将被声明,您只需声明
NotificationView
。希望这能解决
标签中的问题。

我不确定您发布的日志是否属于您的应用程序。你能解释一下当你点击通知时发生了什么吗?你在测试什么版本的Android操作系统?你们在emulator上测试吗?是的,我在android 4.3.1的emulator上测试。当我点击通知图标时,什么也没有发生。它应该会启动我刚刚用模拟器测试过的所需的活动,即使在那里也能正常工作。您是否在状态栏上向下滑动后单击通知?看起来您需要在onCreate方法或runonUIThread中执行大量指令。请考虑做线程,可运行或异步任务为您的长期操作。阅读有关此警告的更多信息“应用程序可能在其主线程上做了太多工作。”
int notificationId = System.currentTimeMillis()/1000;
notificationManager.notify(notificationId, notification);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Intent intentNotif = new Intent(this, NotificationView.class);
intentNotif.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intentNotif, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setAutoCancel(true)
                .setOngoing(false)
                .setContentIntent(pendIntent)
                .setContentTitle("Title here")
                .setContentText("Notification content!")
                .setTicker("This is ticker!");

int id = 1234;
mNotificationManager.notify(id, mBuilder.build());
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".NotificationView"
        android:label="@string/app_name" />
</application>