Java 下拉菜单上的前台服务冻结通知栏

Java 下拉菜单上的前台服务冻结通知栏,java,android,service,Java,Android,Service,我正在开发一个Android应用程序,在那里我启动了一个后台服务,运行时间不定,效果很好,但在做了一些研究之后,我似乎应该将其添加为前台服务,这样它就不会被杀死。我还希望这项服务继续在后台运行,即使应用程序关闭或被杀死。在将前台服务添加到我的服务类之后,我注意到,当你拉下通知栏时,手机会冻结,屏幕会空白几秒钟,然后显示备份。通知栏在加载后按计划工作,但时间太长。我对这些服务还比较陌生,但我开始更好地掌握它们,因此非常感谢您的帮助。已经在运行Anodrid 5.1.1的一加二手机上进行了测试。以下

我正在开发一个Android应用程序,在那里我启动了一个后台服务,运行时间不定,效果很好,但在做了一些研究之后,我似乎应该将其添加为前台服务,这样它就不会被杀死。我还希望这项服务继续在后台运行,即使应用程序关闭或被杀死。在将前台服务添加到我的服务类之后,我注意到,当你拉下通知栏时,手机会冻结,屏幕会空白几秒钟,然后显示备份。通知栏在加载后按计划工作,但时间太长。我对这些服务还比较陌生,但我开始更好地掌握它们,因此非常感谢您的帮助。已经在运行Anodrid 5.1.1的一加二手机上进行了测试。以下是我的应用程序设置:

MainActivity.java

@Override
public void onClick(View v) {
    switch (v.getId()) {
        Intent serviceIntent = new Intent(this, MileageService.class);
    serviceIntent.setAction("start");
    startService(serviceIntent);

    Intent i = new Intent(getApplicationContext(), MapsActivity.class);
    startActivity(i);
    }
}
MileageService.java

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //Everything under here before the final return START_STICKY 
    //was newly added and is the source of the new issues

    if (intent.getAction().equals("start")) {
        if(stringNames.size() == 0) {
            subscribeToLocationUpdates();

            Intent stopIntent = new Intent(this, MileageService.class);
            stopIntent.setAction("stop");
            PendingIntent pStopIntent = PendingIntent.getService(this, 0, stopIntent, 0);

            Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_name);
            Notification notification = new NotificationCompat.Builder(this)
                    .setContentTitle("Mileage Service")
                    .setTicker("Mileage Service")
                    .setContentText("Mileage service is running")
                    .setSmallIcon(R.drawable.android_logo)
                    .setLargeIcon(icon)
                    .setOngoing(true)
                    .addAction(R.drawable.ic_media_pause, "Stop", pStopIntent).build();
            startForeground(101, notification);
        }else{
            return START_STICKY;
        }
    } else if (intent.getAction().equals("stop")) {
        stopForeground(true);
        stopSelf();
    }
    return START_STICKY;
}

public void onLocationChanged(final Location loc) {
    Log.i("***********", "Location changed");
    //I also do code here that updates global variables 
    //that get put into an intent to be sent back to app in further 
    //activities but not showing it as it works fine before adding 
    //notification foreground code

    intent.putExtra("names", stringNames);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);        
}

这听起来像是你的手机出了问题,应用程序做不了什么。如果我删除了通知代码,它就会正常运行,这让我产生了相反的想法。我已经在运行安卓5.1.1的1+2上测试并运行了这个,只是为了进一步了解。你的图标有多大?它们的大小是否符合Android标准?我刚才注意到Android_的徽标是1600 x 1600。我将替换它并从那里开始。我知道标准只是从未检查默认图标集。将使用我的结果更新。是的,将图像解码为位图确实需要CPU。。。