CJ_Finger 2016-12-30 08:26 采纳率: 100%
浏览 1294

关于安卓Service通知栏提醒的问题

各位大侠好,我的Service能运行,输出也有,但就是通知栏提醒的部分无效,网上的代码都尝试了几遍,都不行,下面是代码:

NofyService.java

 public class NofyService extends Service {
    private final String TAG = "NofyService";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
        builder.setContentTitle("Bmob Test");
        builder.setContentText("message");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(intent);//执行intent
        Notification notification = builder.getNotification();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        manager.notify(1,notification);
        Log.v(TAG, "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Calendar ca = Calendar.getInstance();
        long[] vibrates = {0, 1000, 500, 1000};
        Notification notification = new Notification.Builder(getApplicationContext())
                .setContentTitle("事件提醒")
                .setContentText("有事件")
                .setVibrate(vibrates)
                .build();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(1, notification);
        Log.v(TAG, "onStart");
        return START_STICKY;
    }

}

onCreate 和 onStart 的 Log 都有输出,就是没看到虚拟机的通知栏有消息,请问这是什么问题?

  • 写回答

3条回答 默认 最新

  • 代码的灵魂是bug! 2016-12-30 08:29
    关注

    用真机测试一下看看有没有

    评论

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题