开发者社区> 问答> 正文

有关于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

展开
收起
爵霸 2016-03-10 08:33:49 2152 0
1 条回答
写回答
取消 提交回答
  • 那个不能用了 你用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);
    2019-07-17 18:56:41
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载