2 a460670026 a460670026 于 2016.03.23 15:05 提问

关于Android中Notification问题

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

2个回答

u011249920
u011249920   2016.03.23 15:19
已采纳

setLatestEventInfo方法已经被deprecate啦,不建议使用,使用Notification.Builder即可。

a460670026
a460670026 搞定了,谢谢
一年多之前 回复
a460670026
a460670026 就修改这个对象方法,其他写法改动大吗?
一年多之前 回复
lxk_1993
lxk_1993   Rxr 2016.03.23 18:30

那个不能用了 你用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);

a460670026
a460670026 搞定了,谢谢
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!