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
那个不能用了 你用RemoteViews
像这样
Notification n = new Notification();
n.icon = R.drawable.message;
n.tickerText = "你的好友给你发来一条消息";
n.when = System.currentTimeMillis();
n.defaults = Notification.DEFAULT_SOUND;
n.flags = Notification.FLAG_AUTO_CANCEL;
Intent intent1 = new Intent(MainActivity.this, ShowActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 110, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.test);
remoteViews.setTextViewText(R.id.text, "在不在?");
remoteViews.setImageViewResource(R.id.img, R.drawable.wallet);
remoteViews.setOnClickPendingIntent(R.id.text, pi);
n.contentView = remoteViews;
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(110, n);
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。