自有服务:无法实例化接收者

时间:2017-08-22 23:14:11

标签: java android service

首先,我必须说我是java的全新手。 我用一些教程写了这篇文章。

如您所见,清单中的包名称是正确的,并且没有拼写错误......

我正在尝试为我的设备编写服务,它控制着它的导向。但我一直收到这个错误:

AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.gabrielgagz.ledpearlyn.LedPearlynReceiver" on path: DexPathList[[zip file "/system/app/LedPearlyn/LedPearlyn.apk"],nativeLibraryDirectories=[/system/app/LedPearlyn/lib/arm, /system/app/LedPearlyn/LedPearlyn.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib, /system/lib, /vendor/lib]]

LedPearlynReceiver:

package com.gabrielgagz.ledpearlyn;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.io.*;

public class LedPearlynReceiver extends BroadcastReceiver {

public static boolean wasScreenOn = true;

@Override
public void onReceive(final Context context, final Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) || intent.getAction().equals(Intent.ACTION_DREAMING_STARTED))  {
        try {
        String[] cmd = { "/system/bin/sh", "-c", "echo 0 > /sys/class/leds/pearlyn/brightness"};         
        Runtime.getRuntime().exec(cmd);
        } catch(IOException ie) {
        ie.printStackTrace();
        }
        wasScreenOn = false;
        Log.e("LedPearlyn","OFF");
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON) ||  intent.getAction().equals(Intent.ACTION_DREAMING_STOPPED)) {
        try {
        String[] cmd = { "/system/bin/sh", "-c", "echo 255 > /sys/class/leds/pearlyn/brightness"};       
        Runtime.getRuntime().exec(cmd);
        } catch(IOException ie) {
        ie.printStackTrace();
        } 
        wasScreenOn = true;
        Log.e("LedPearlyn","ON");
    }        
}
}

LedPearlynService:

package com.gabrielgagz.ledpearlyn;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.content.BroadcastReceiver;

public class LedPearlynService extends Service {

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

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_DREAMING_STARTED);
    filter.addAction(Intent.ACTION_DREAMING_STOPPED);
    final BroadcastReceiver mReceiver = new LedPearlynReceiver();
    registerReceiver(mReceiver, filter);
    return super.onStartCommand(intent, flags, startId);
    }
public class LocalBinder extends Binder {
    LedPearlynService getService() {
    return LedPearlynService.this;
    }
}
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest android:sharedUserId="android.uid.system" android:versionCode="1" android:versionName="1.0" package="com.gabrielgagz.ledpearlyn" platformBuildVersionCode="25" platformBuildVersionName="7.1.2"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
<application>
    <receiver android:label="LedPearlynReceiver" android:name=".LedPearlynReceiver" android:exported="false"> 
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.SCREEN_ON" />
        <action android:name="android.intent.action.SCREEN_OFF" />
        <action android:name="android.intent.action.DREAMING_STARTED" />
        <action android:name="android.intent.action.DREAMING_STOPPED" />
    </intent-filter>
    </receiver>
    <service android:name=".LedPearlynService" android:exported="false"></service>
</application>
</manifest>

1 个答案:

答案 0 :(得分:0)

您需要从Manifest中的android:exported="false"声明中删除LedPearlynReceiver(默认情况下,具有intent过滤器的接收器具有android:exported = true)。

参考:https://developer.android.com/guide/topics/manifest/receiver-element.html#exported

另外,尝试在清单中添加完全限定名称。 android:name="com.gabrielgagz.ledpearlyn.LedPearlynReceiver"