点击通知栏当中的条目跳到一个新的页面,这里我们先写一个other_activity.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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本框"/>
</LinearLayout>
OtherActivity.java
package com.example.notificationtest;
import android.os.Bundle;
import android.app.Activity;
public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity);
}
}
在AndroidManifest.xml当中加入activity,并且加入权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- 添加操作闪光灯的权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!-- 添加操作震动的权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.notificationtest.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="com.example.notificationtest.OtherActivity"
android:label="@string/other_activity">
</activity>
</application>
</manifest>
main_activity.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"
tools:context=".MainActivity"
android:orientation="vertical">
<Button android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示通知栏"/>
<Button android:id="@+id/cancle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消通知栏"/>
</LinearLayout>
MainActivity.java
package com.example.notificationtest;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
final int NOTIFICATION_ID=0X123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button show=(Button) super.findViewById(R.id.show);
Button cancle=(Button) super.findViewById(R.id.cancle);
show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//安卓组件之间的通讯,主要是由Intent完成的
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
/*
* pendingIntent是一种特殊的Intent。主要的区别在于Intent的执行立刻的,而pendingIntent的执行不是立刻的。
*/
//当点击消息的时候进行跳转
PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
//创建一个Notification
Notification notify=new Notification();
//设置图标
notify.icon=R.drawable.ic_launcher;
//设置弹出时候的文本内容
notify.tickerText="您有一条新消息";
//为Notification设置发送时间
notify.when=System.currentTimeMillis();
//为Notification设置声音
notify.defaults=Notification.DEFAULT_SOUND;
//设置默认震动
//notify.defaults=Notification.DEFAULT_VIBRATE;
//以重复的方式1秒震动、1秒停止,共4秒动
long[] vibrate={1000,1000,1000,1000};
notify.vibrate=vibrate;
notify.defaults=Notification.DEFAULT_ALL;
//设置闪光灯的颜色为红色
//notify.ledARGB=Color.RED;
/*
* ledOffMS 0让闪光灯一直亮着
* ledOnMS ledOffMS都设为0表示的是关闭此功能
*/
//开始闪光灯的时间
//notify.ledOnMS=1;
//关闭闪光灯的时间
//notify.ledOffMS=0;
//notify.flags=Notification.FLAG_SHOW_LIGHTS;
//设置事件信息
/*
*普通通知,显示在通知栏的标题
*点击查看是副标题
*/
notify.setLatestEventInfo(MainActivity.this, "普通通知","点击查看", pi);
//获取系统的NotificationManager服务
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//发送通知
notificationManager.notify(NOTIFICATION_ID, notify);
}
});
cancle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 获取系统的NotificationManager服务
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//取消通知
notificationManager.cancel(NOTIFICATION_ID);
}
});
}
}
6223

被折叠的 条评论
为什么被折叠?



