public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt1=(Button) findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//通知消息與Intent關聯
Intent i=new Intent(MainActivity.this, NotifiedActivity.class);
PendingIntent pi=PendingIntent.getActivity(MainActivity.this, 0, i, 0);
Notification mynNotification=new Notification(); //定義notification
mynNotification.icon=R.drawable.gender;
mynNotification.tickerText=getResources().getString(R.string.notification);
mynNotification.defaults=Notification.DEFAULT_SOUND;
//具體的通知內容
mynNotification.setLatestEventInfo(MainActivity.this, "示例", "點擊查看",pi);
//從系統服務中獲得通知管理器
NotificationManager nm=(NotificationManager)MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
//執行通知
nm.notify(0, mynNotification);
}
});
}
}
//就是我想點擊按鈕之後在狀態欄出現通知,然後點擊通知會跳轉另一個頁面,但就是跳不過去。 求大神支招。
//mynNotification.setLatestEventInfo(MainActivity.this, "示例", "點擊查看",pi);
//上面這句代碼出現警告The method setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent) from the type Notification is deprecated
setLatestEventInfo方法已經被deprecate啦,不建議使用,使用Notification.Builder即可。