问题描述
- 关于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
解决方案
setLatestEventInfo方法已经被deprecate啦,不建议使用,使用Notification.Builder即可。
解决方案二:
那个不能用了 你用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);