(1
)使用系统定义的Notification
MainActivity.class
package com.example.test;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity implements OnClickListener{
private Button btn_notification;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initViews();
initListener();
}
private void initViews(){
btn_notification = (Button) findViewById(R.id.btn_notification);
}
private void initListener(){
btn_notification.setOnClickListener(this);
}
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public void onClick(View v){
int id = v.getId();
if(id == R.id.btn_notification){
// 新版本的通知的使用
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
new Intent(getApplicationContext(),NotificationJumpActivity.class),
0);
Builder builder = new Notification.Builder(getApplicationContext());
builder.setContentTitle("通知的标题"); // 设置下拉列表里面的标题
builder.setContentText("通知的内容"); // 设置上下文内容
builder.setWhen(System.currentTimeMillis()); // 设置时间
builder.setAutoCancel(true); // 设置可以清除
// 设置状态栏里通知的图标,不知道是不是手机问题,当把这句注释掉时,能收到通知,但是在状态栏里看不到这条通知,在下拉列表中也不会看到这条通知。
builder.setSmallIcon(this.getApplicationInfo().icon);
// 设置 下拉消息时,消息左边的图标,这句注释掉的话呢,通知栏里会有,下拉里面就没有了哦
builder.setLargeIcon(BitmapFactory.decodeResource(this.getApplicationContext().getResources(), R.drawable.ic_launcher));
builder.setTicker("这是状态栏的显示");
builder.setContentIntent(pendingIntent);
@SuppressWarnings("deprecation")
Notification notification = builder.getNotification();
// Notification notification = builder.build();// API 16 以上使用 即 Android 4.1.2版本
notification.defaults = Notification.DEFAULT_SOUND; // 默认声音
notification.flags = Notification.FLAG_AUTO_CANCEL; // 自动终止
notification.contentIntent = pendingIntent;
nm.notify(0, notification);
// // 旧版本的通知的使用
// NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Notification notification = new Notification(R.drawable.ic_launcher,"我的通知",System.currentTimeMillis());
// notification.defaults = Notification.DEFAULT_SOUND;
// notification.tickerText = "这是状态栏通知内容";
// notification.icon = R.drawable.ic_launcher;
// notification.setLatestEventInfo(getApplicationContext(), "我是标题", "我是内容",
// PendingIntent.getActivity(getApplicationContext(), 0, new Intent(MainActivity.this,NotificationJumpActivity.class), 0));
// nm.notify(0, notification);
}
}
}
activity_main.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" >
<Button
android:id="@+id/btn_notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text_notification"
/>
</LinearLayout>
NotificationJumpActivity.class
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class NotificationJumpActivity extends Activity{
private TextView tv_content;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_jump);
initViews();
}
private void initViews(){
tv_content = (TextView) findViewById(R.id.tv_content);
tv_content.setText(R.string.jump_tv_content);
}
}
activity_jump.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/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
strings.xml
<resources>
<string name="app_name">Test</string>
<string name="text_notification">通知</string>
<string name="jump_tv_content">这是从通知状态栏跳转过来打开的Activity</string>
</resources>