无法绑定到前台服务

时间:2019-06-20 18:53:00

标签: android

我已经实现了作为前台服务运行的服务。但是,我的活动无法绑定到它:

清单

<service
    android:name=".service.MyService"
    android:enabled="true"
    android:exported="true" />

应用类

public class MyApplication extends Application {

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

        Intent intent = new Intent(this, MyService.class);
        intent.setAction(MyService.ACTION_START_FOREGROUND_SERVICE);
        ContextCompat.startForegroundService(this, intent);
    }
}

AIDL

package com.my.app;
interface IMyService {

    boolean isReady();
// and there's more methods
}

服务

private IMyService.Stub binder;

public MyService() {
    binder = new IMyService.Stub() {
        @Override
        public boolean isReady() throws RemoteException {
            return mySdk != null;
        }

        // ... more methods ...

    };
}

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

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate()");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if(intent != null) {
        String action = intent.getAction();
        if (action == null) action = ACTION_START_FOREGROUND_SERVICE;

        switch (action) {
            case ACTION_START_FOREGROUND_SERVICE:
                Toast.makeText(getApplicationContext(), "Service is started", Toast.LENGTH_LONG).show();
                startForegroundService();
                break;
            case ACTION_STOP_FOREGROUND_SERVICE:
                Toast.makeText(getApplicationContext(), "Service is stopped", Toast.LENGTH_LONG).show();
                stopForegroundService();
                break;
        }
    }
    return START_STICKY;
}

private void startForegroundService() {
    Log.d(TAG_FOREGROUND_SERVICE, "startForegroundService() starting");

    Intent intent = new Intent();
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    String channelId = "your_channel_id";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence channelName = "Your Channel";
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.BLUE);
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId);

    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle("My service");
    bigTextStyle.bigText("My Service is active");

    builder.setStyle(bigTextStyle);
    builder.setWhen(System.currentTimeMillis());
    builder.setSmallIcon(R.mipmap.ic_launcher);
    Bitmap largeIconBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    builder.setLargeIcon(largeIconBitmap);
    builder.setPriority(NotificationManager.IMPORTANCE_LOW);
    builder.setFullScreenIntent(pendingIntent, true);

    Notification notification = builder.build();

    startForeground(STOP_FOREGROUND_REMOVE, notification);

    Log.d(TAG_FOREGROUND_SERVICE, "startForegroundService() complete");
}

活动

@Override
public void onStart() {
    super.onStart();
    if (serviceBinder == null) {
        handler.post(() -> doBindService());
    }
}

public void doBindService() {
    Intent intent = new Intent();
    intent.setClassName("com.my.app.service","com.my.app.service.MyService");
    boolean success = bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
    Log.d(TAG,"success = "+success);
}

public ServiceConnection myConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder binder) {
        MyServiceBinder = IMyService.Stub.asInterface(binder);
        Log.d(TAG,"connected");
        serviceConnected();
    }

    public void onServiceDisconnected(ComponentName className) {
        Log.d(TAG,"disconnected");
        MyServiceBinder = null;
    }
};

LOGCAT输出

14883-14883 D / MyService:onCreate()

14883-14883 D / MyService:startForegroundService()开始

14883-14883 D / MyService:startForegroundService()已完成

14883-14883 D / MyActivity:成功=假

有什么我想念的吗? bindService()不断返回false

1 个答案:

答案 0 :(得分:0)

我注意到在代码的“活动”部分中,应用程序的名称空间有误

应该是“ com.my.app”而不是“ com.my.app.service”

下面是固定代码:

public void doBindService() {
    Intent intent = new Intent();
    intent.setClassName("com.my.app","com.my.app.service.MyService");
    boolean success = bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
    Log.d(TAG,"success = "+success);
}