关于通知的一些简单的设置和方法介绍
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.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
/**
*
- @项目名:NotificationTest_demo1
- @类名称:MainActivity
- @类描述:通知基本方法
- @创建人:HXF
- @修改人:
- @创建时间:2015-8-13 上午10:20:04
- @version
*/
public class MainActivity extends Activity implements OnClickListener {
private Button sendNotice;
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
sendNotice = (Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_notice:
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(
R.drawable.ic_launcher, "这是Notification通知",
System.currentTimeMillis());
/** 实现通知能被点击并跳转界面 */
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
long[] vibrate = {0,1000,1000,1000};
/** 发送通知的让手机震动的时间设置 */
notification.vibrate = vibrate;
/** 可以设置通知来的时候有LED提示灯显示亮度 */
notification.ledARGB = Color.GREEN;
notification.ledOnMS = 1000;
notification.ledOffMS = 1000;
notification.flags = Notification.FLAG_SHOW_LIGHTS;
/** 一行搞定所有以上繁琐工作(设置声音,设置震动,设置LED光亮) */
notification.defaults = Notification.DEFAULT_ALL;
notification.setLatestEventInfo(this, "这是Notification通知标题",
"这是Notification通知内容", pi);
/** 1代表id,可供后面取消的时候使用 */
manager.notify(1, notification);
break;
default:
break;
}
}
}
实现点击通知的界面的跳转,并且取消通知栏中的通知。
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
public class NotificationActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
/** 这边取消通知栏的通知,实在被跳转的界面*/
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
/** 1代表id,这里传入1是要取消MainActivity方法中的发出的那个通知 */
manager.cancel(1);
}
}
MainActivity中的布局
<Button
android:id="@+id/send_notice"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#6BCBDF"
android:text="Send notice" />
Notification中的布局都贴出来,希望能帮助初学者:
<?xml version="1.0" encoding="utf-8"?><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="这里是Notification布局"
android:textSize="24sp" />