写了如下代码
package com.example.administrator.notification;
import ...
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendnotice = (Button)findViewById(R.id.send_notice);
sendnotice.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.send_notice:
Intent intent =new Intent(this,NotifiacationActivity.class);
PendingIntent pi =PendingIntent.getActivity(this,0,intent,0);
NotificationManager manager = (NotificationManager) getSystemService
(Context.NOTIFICATION_SERVICE);
Notification notification =new NotificationCompat.Builder(this)
.setContentTitle("这是标题")
.setContentText("这是内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher))
.setContentIntent(pi)
.build();
manager.notify(1,notification);
break;
default:
break;
}
}
}发现测试的点击Button,不能发送通知,是怎么回事