求助。Notification问题,程序一运行就崩溃,找不到原因。
android开发吧
全部回复
仅看楼主
level 3
暖冬阳😁 楼主
package com.LiuPan.liupan_notification;
import android.support.v7.app.ActionBarActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
private Notification notification;
private NotificationManager manager;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification.Builder(this)
.setTicker("接收到一个Notification")
.setContentTitle("Notification")
.setContentText("这是一条Notification")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND)
.setSmallIcon(android.R.drawable.ic_notification_clear_all)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setWhen(System.currentTimeMillis())
.build();
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
manager.notify(1, notification);//发送一条通知
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
2015年02月19日 08点02分 1
level 11
看不清
2015年02月19日 09点02分 2
level 6
看不太清,好像是没有找到方法的错误
2015年02月19日 10点02分 3
是没找到方法,但是那个notification的build方法难道不再Notification.builder里面?
2015年02月19日 11点02分
1