当前位置: 代码迷 >> Android >> android notification范例
  详细解决方案

android notification范例

热度:96   发布时间:2016-05-01 17:22:44.0
android 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">	<Button android:text="Button" android:id="@+id/button1"		android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></LinearLayout>


两个Activity
package com.tcl.testnotifycation;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class TestNotificationActivity extends Activity {	private NotificationManager mNotificationManager;	private PendingIntent mPendingIntent;	/** Called when the activity is first created. */	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);		// 点击通知切换到Activity2		Intent intent = new Intent(this, Activity2.class);		mPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);		Button button = (Button) findViewById(R.id.button1);		button.setOnClickListener(new OnClickListener() {			@Override			public void onClick(View v) {				Notification notification = new Notification();				notification.icon = R.drawable.icon;// 显示图标				notification.tickerText = "test...";// 点击通知用户的内容				notification.defaults = Notification.DEFAULT_SOUND;// 默认声音				// 通知时震动				// notification.defaults = Notification.DEFAULT_VIBRATE;				// 通知时屏幕发亮				// notification.defaults = Notification.DEFAULT_LIGHTS;				notification.setLatestEventInfo(TestNotificationActivity.this,						"myNotification", "test test test", mPendingIntent);				mNotificationManager.notify(0, notification);			}		});	}}

package com.tcl.testnotifycation;import com.tcl.testnotifycation.R.layout;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.LinearLayout;import android.widget.TextView;public class Activity2 extends Activity{	public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        LinearLayout layout = new LinearLayout(this);        TextView textView = new TextView(this);        textView.setText("this is test notifycation");        layout.addView(textView);        setContentView(layout);	}}
  相关解决方案