2017-08-22 1 views
0

Tout d'abord, je dois dire que je suis totalement novice sur Java. J'ai écrit ceci en utilisant quelques tutoriels.Propre service: impossible d'instancier le récepteur

Comme vous le voyez ,, le nom du package est correct dans le manifeste et il n'y a pas orthographier erreurs ...

Je suis en train d'écrire un service pour mon appareil, Wich contrôle la led de celui-ci. Mais je suis continuer à obtenir cette erreur:

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> 

Répondre

0

Vous devez supprimer android:exported="false" (par de défaut pour les récepteurs avec des filtres d'intention a android: exported = true) à partir de la déclaration LedPearlynReceiver dans le manifeste.

Référence: https://developer.android.com/guide/topics/manifest/receiver-element.html#exported

Aussi, essayez d'ajouter le nom complet dans le manifeste.

+0

Merci pour votre réponse, mais toujours le même problème même avec la solution que vous avez fournie ... – gabrielgagz

+0

Mis à jour la réponse. –

+0

Merci encore! Mais non, ça ne marche pas ... – gabrielgagz