安卓,怎么不能发送通知到状态栏上?

Email.java

 package com.examp.notificationdemof;

import java.io.Serializable;

public class Email implements Serializable{
    private String title,content;

    public Email(String title,String content)
    {
        this.title=title;
        this.content=content;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title=title;
    }

    public String getContent()
    {
        return content;
    }

    public void setContent(String content)
    {
        this.content=content;
    }


}

EmailActivity.java

 package com.examp.notificationdemof;

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

public class EmailActivity extends Activity{
    private TextView tvTitle=null;
    private TextView tvContent=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email);
        tvTitle=(TextView)findViewById(R.id.tvTiTle);
        tvContent=(TextView)findViewById(R.id.tvContent);
        Bundle bundle=getIntent().getExtras();
        if (bundle==null)
            return;
        Email email=(Email)bundle.getSerializable("email");
        if (email!=null)
        {
            tvTitle.setText(email.getTitle());
            tvContent.setText(email.getContent());
        }
    }


}

MainActivity.java

 package com.examp.notificationdemof;

import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
    private NotificationManager notificationManager;
    private Button bt1=null;
    private Button bt2=null;
    private final int NOTIFICATION_ID = 0x123;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        bt1=(Button)findViewById(R.id.bt_send);
        bt1.setOnClickListener(this);
        bt2=(Button)findViewById(R.id.bt_cancel);
        bt2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_send:
            String title="Hello Android";
            String content="welcome to the Android world";
            Email email=new Email(title,content);
            Intent intent=new Intent(MainActivity.this,EmailActivity.class);
            Bundle bundle=new Bundle();
            bundle.putSerializable("email", email);
            intent.putExtras(bundle);
            startActivity(intent);
            PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            Builder builder=new Notification.Builder(this);
            builder.setTicker("You got a mail");
            builder.setContentTitle(email.getTitle());
            builder.setContentText(email.getContent());
            builder.setAutoCancel(true);
            builder.setContentIntent(pendingIntent);
            Notification notification=builder.build();
            notificationManager.notify(NOTIFICATION_ID,notification);
            break;
        case R.id.bt_cancel:
            notificationManager.cancel(NOTIFICATION_ID);
            break;
        default:break;
        }
    }


    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这个是不是手机权限的问题,检查一下通知权限。

activity_email.xml

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

    <TextView
        android:id="@+id/tvTiTle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

activity_main.xml

 <LinearLayout 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="com.examp.notificationdemof.MainActivity"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bt_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/send"
        android:textSize="20sp"  />

    <Button
        android:id="@+id/bt_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textSize="20sp"  />

 </LinearLayout>

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <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=".EmailActivity" >
        </activity>

    </application>

</manifest>

不懂,我也写了一个,但也没有出现,是不是因为6.0系统有所改变的问题呢,以前写的demo现在也不能出现通知了

 package com.exampe.notificationdem;

import java.util.Random;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
    private Button bt1=null;
    private Button bt2=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1=(Button)findViewById(R.id.bt_send);
        bt1.setOnClickListener(this);
        bt2=(Button)findViewById(R.id.bt_cancel);
        bt2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_send:
            NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification=new Notification();
            notification.flags=Notification.FLAG_ONGOING_EVENT;
            notification.tickerText="一个通知";
            notification.when=System.currentTimeMillis();
            Intent intent=new Intent(MainActivity.this,MainActivity.class);
            //ComponentName cn=new ComponentName("com.exampe.notificationdem","com.exampe.notificationdem.MainActivity");
            //intent.setComponent(cn); 
            PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, (new Random().nextInt(100)),intent, PendingIntent.FLAG_UPDATE_CURRENT);
            notification.setLatestEventInfo(this,"title","a message",pendingIntent);
            notificationManager.notify(R.layout.activity_main,notification);
            break;
        case R.id.bt_cancel:
            NotificationManager notificationManage=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManage.cancel(R.layout.activity_main);
            break;
        default:break;
        }
    }


    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 package com.examp.notificationdemof;

import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{
    private Button bt1=null;
    private Button bt2=null;
    private final int NOTIFICATION_ID = 0x123;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1=(Button)findViewById(R.id.bt_send);
        bt1.setOnClickListener(this);
        bt2=(Button)findViewById(R.id.bt_cancel);
        bt2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_send:
            NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            String title="Hello Android";
            String content="welcome to the Android world";
            Email email=new Email(title,content);
            Intent intent=new Intent(MainActivity.this,EmailActivity.class);
            ComponentName cn=new ComponentName("com.examp.notificationdemof","com.examp.notificationdemof.EmailActivity");
            intent.setComponent(cn);  
            Bundle bundle=new Bundle();
            bundle.putSerializable("email", email);
            intent.putExtras(bundle);
            PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            Builder builder=new Notification.Builder(this);
            builder.setTicker("You got a mail");
            builder.setSmallIcon(R.drawable.ic_launcher);
            builder.setContentTitle(email.getTitle());
            builder.setContentText(email.getContent());
            builder.setAutoCancel(true);
            builder.setContentIntent(pendingIntent);
            Notification notification=builder.build();
            notificationManager.notify(NOTIFICATION_ID,notification);
            startActivity(intent);
            break;
        case R.id.bt_cancel:
            NotificationManager notificationManage=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManage.cancel(NOTIFICATION_ID);
            break;
        default:break;
        }
    }


    @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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

图片说明