main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:textSize="20sp" android:layout_centerInParent="true" /> </RelativeLayout>
another.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/newActivity" android:textSize="20sp" android:layout_centerInParent="true" /> </RelativeLayout>
MainActivity如下:
import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; /** * Demo描述:发送系统通知 * 注意: * 1 PendingIntent的使用 * 2 权限申请 * */ public class MainActivity extends Activity { private Button mButton; private final int FIRST=1; private final int SECOND=2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mButton=(Button) findViewById(R.id.button); mButton.setOnClickListener(new ButtonOnClickListenerImpl()); } private class ButtonOnClickListenerImpl implements OnClickListener{ @Override public void onClick(View v) { NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent firstIntent=new Intent(); PendingIntent firstPendingIntent=PendingIntent.getActivity(MainActivity.this, 0, firstIntent, 0); Notification firstNotification=new Notification(); //设置通知的图标 firstNotification.icon=R.drawable.ic_launcher; //设置状态栏的通知提示信息 firstNotification.tickerText=getResources().getString(R.string.firstNotification_ticker); //设置通知发送时间 firstNotification.when=System.currentTimeMillis(); //采用系统默认的声音,震动,闪光灯 firstNotification.defaults=Notification.DEFAULT_ALL; //打开应用程序后图标消失 firstNotification.flags|=Notification.FLAG_AUTO_CANCEL; //设置通知的标题和内容,以及点击通知后的相应操作.此处无操作. //注意:如果将该方法最后一个参数设置为null,则报错 firstNotification.setLatestEventInfo(MainActivity.this, getResources().getString(R.string.firstNotification_title), getResources().getString(R.string.firstNotification_content), firstPendingIntent); //发送第一个通知 manager.notify(FIRST, firstNotification); Intent secondIntent=new Intent(MainActivity.this,AnotherActivity.class); PendingIntent secondPendingIntent=PendingIntent.getActivity(MainActivity.this, 0, secondIntent, 0); Notification secondNotification=new Notification(); //设置通知的图标 secondNotification.icon=R.drawable.ic_launcher; //设置状态栏的通知提示信息 secondNotification.tickerText=getResources().getString(R.string.secondNotification_ticker); //设置通知发送时间 secondNotification.when=System.currentTimeMillis(); //采用系统默认的声音,震动,闪光灯 secondNotification.defaults=Notification.DEFAULT_ALL; //打开应用程序后图标消失 secondNotification.flags|=Notification.FLAG_AUTO_CANCEL; //设置通知的标题和内容,以及点击通知后的相应操作.此处为打开指定的Activity //注意:如果将该方法最后一个参数设置为null,则报错 secondNotification.setLatestEventInfo(MainActivity.this, getResources().getString(R.string.secondNotification_title), getResources().getString(R.string.secondNotification_content), secondPendingIntent); //发送第二个通知 manager.notify(SECOND, secondNotification); } } }
AnotherActivity如下:
import android.app.Activity; import android.os.Bundle; public class AnotherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.another); } }