通知的安卓代码:
public class MyService extends Service {
…
public void onCreate() {
super.onCreate();
Log.d(“Myservice”, "onCreate execute ");
Intent intent = new Intent(this,MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
Notification notification =new NotificationCompat.Builder(this)
.setContentTitle(“This is content title”)
.setContentTitle(“This is content text”)
.setTicker(“You have import thing to do it”)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentIntent(pi)
.build();
startForeground(1,notification);
}
}
这是安卓的服务中的代码,在活动MainActivity内要启动MyService:
case R.id.button2:
Intent intent = new Intent(this, MyService.class);
startService(intent);
Android 使用消息通知 不显示可能的原因
最新推荐文章于 2023-05-11 15:31:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)