0

I am trying to learn android on my own, and currently working on a simple project.

I want to receive whenever the phone is plugged to a power source, so I put a receiver to the manifest and create a subclass under my main activity. The problem is the program crashes when I plug the phone.

There are many questions about it here and everywhere else, and believe me I looked all of them but none of them solved this problem.

MainActivity.java

public class MySystemBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "ACTION_POWER_CONNECTED event received", Toast.LENGTH_LONG).show();
    }
}

Manifest:

    <receiver
        android:name="deneme.sarjolcer.MainActivity$MySystemBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        </intent-filter>
    </receiver>

Please forgive me if there is a too silly error, I am just new at this thing. I have been working over the same error for a week now and actually do not see straight now. Thank you very much.

cskr
  • 77
  • 2
  • 9
  • check this stackoverflow answer http://stackoverflow.com/questions/6217692/detecting-the-device-being-plugged-in – Marco Luongo Dec 20 '15 at 22:47
  • @NorbertvanNobelen appearently, inner classes must be written with $ sign instead of a dot. Android Studio giving me an error ptherwise. – cskr Dec 20 '15 at 22:57
  • @MarcoLuongo I want to create the receiver in the manifest, that question uses dynamic receiver. When I implement everything else in there, result does not change – cskr Dec 20 '15 at 23:00
  • Give us stack trace. We need to see error in order to see what went wrong... – Bozic Nebojsa Dec 20 '15 at 23:37
  • @BozicNebojsa Really sorry but I'm a newbie, so please help me, how can I print the trace of the program? (Note that I cannot debug it since I have to unplug the phone from the pc and re-plug it. ) – cskr Dec 20 '15 at 23:58

1 Answers1

0
add permission 
<uses-permission android:name="android.permission.BATTERY_STATS"></uses-permission>
Marco Luongo
  • 397
  • 4
  • 13