android通知应用程序无法正常工作

时间:2015-11-18 14:43:05

标签: android android-layout android-intent android-activity

你好,我最近启动了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>

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="MainActivity">

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Notification Example"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point "
      android:textColor="#ff87ff09"
      android:textSize="30dp"
      android:layout_below="@+id/textView1"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="48dp" />

   <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageButton"
      android:src="@drawable/ic_launcher"
      android:layout_below="@+id/textView2"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="42dp" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Notification"
      android:id="@+id/button"
      android:layout_marginTop="62dp"
      android:layout_below="@+id/imageButton"
      android:layout_centerHorizontal="true" />

</RelativeLayout>

notification.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >

   <TextView
      android:layout_width="fill_parent"
      android:layout_height="400dp"
      android:text="Hi, Your Detailed notification view goes here...." />
</LinearLayout>

这是点击通知图标

期间的logcat数据
11-19 06:05:44.924: I/Choreographer(1214): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-19 06:05:44.955: V/PanelView(1271): animationTick called with dtms=0; nothing to do (h=0.19166172 v=0.0)
11-19 06:05:44.955: I/Choreographer(1271): Skipped 152 frames!  The application may be doing too much work on its main thread.
11-19 06:05:45.004: V/PanelView(1271): animationTick called with dtms=0; nothing to do (h=0.04166171 v=-1500.0)
11-19 06:05:45.014: V/PanelBar(1271): onTouch: no panel for touch at (14,11)
11-19 06:05:45.024: I/Choreographer(1271): Skipped 69 frames!  The application may be doing too much work on its main thread.
11-19 06:05:45.054: I/Choreographer(1214): Skipped 39 frames!  The application may be doing too much work on its main thread.
11-19 06:05:45.084: I/Choreographer(1271): Skipped 66 frames!  The application may be doing too much work on its main thread.
11-19 06:05:45.154: W/InputMethodManagerService(1214): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@b4e4dbd8 attribute=null, token = android.os.BinderProxy@b4a95d50
11-19 06:05:45.164: I/Choreographer(1271): Skipped 59 frames!  The application may be doing too much work on its main thread.
11-19 06:05:45.174: I/Choreographer(1214): Skipped 53 frames!  The application may be doing too much work on its main thread.
11-19 06:06:00.170: I/Choreographer(1271): Skipped 36 frames!  The application may be doing too much work on its main thread.

4 个答案:

答案 0 :(得分:0)

不要制作静态通知ID。这是个坏主意。

notificationManager.notify(9999, notification);

创建类似的东西,例如:

int notificationId = System.currentTimeMillis()/1000;
notificationManager.notify(notificationId, notification);

它一直对我有用。有关pendingIntent example

的通知的更多信息

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

答案 1 :(得分:0)

尝试使用NotificationCompat,你会在不同设备上得到更好的支持,我写了这篇文章:http://alexzh.com/tutorials/notification-for-android/

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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

我想您可能错过了NotificationView文件中的AndroidManifest.xml声明。必须在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>

MainActivity已经宣布,您只需声明NotificationView即可。希望这能解决<activity>标记中的问题。