0

Here's how I wrote my app.

MusicList

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.music_layout);
    startService(new Intent(getApplicationContext(), LockScreenService.class));

    //other codes

    });

//send chosen music
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            if(lockIsChosen!=null) {
                //other codes
                try {
                    Intent i = new Intent("my.action");
                    i.putExtra("posLock", newPosition2).putExtra("songlistLock", mySongs).putExtra("lockSound", "lock");
                    sendBroadcast(i);
                }catch (Exception e) {
                    Log.e(TAG, "Intent error");
                }
                finish();

            }
            if(unlockIsChosen!=null) {

                //other codes
                try {
                    Intent i = new Intent("my.action.unlock");
                    i.putExtra("posUnlock", newPosition3).putExtra("songlistUnlock", mySongs).putExtra("unlockSound", "unlock");
                    sendBroadcast(i);
                }catch (Exception e) {
                    Log.e(TAG, "Intent error2");
                }

                finish();
            }
        }
    });

And in my service class, this is what I wrote

LockScreenService

 public class LockScreenService extends Service {

MediaPlayer mp;
ArrayList<File> mySongs;
ArrayList<File> mySongs2;
Uri u;
Uri u2;
AudioManager am;
BroadcastReceiver receiver;

public class LockScreenReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

    if(action.equals(Intent.ACTION_BOOT_COMPLETED)) {
        Intent service = new Intent(context, LockScreenService.class);
        context.startService(service);
    }

    if(action.equals("my.action")) {
            Bundle b = intent.getExtras();
            mySongs = (ArrayList) b.getParcelableArrayList("songlistLock");
            int position = b.getInt("posLock", 0);

            u = Uri.parse(mySongs.get(position).toString());
        }

        if(action.equals("my.action.unlock")) {
            Bundle b = intent.getExtras();
            mySongs2 = (ArrayList) b.getParcelableArrayList("songlistUnlock");
            int position = b.getInt("posUnlock", 0);

            u2 = Uri.parse(mySongs2.get(position).toString());
        }

    if (action.equals(Intent.ACTION_SCREEN_ON) && am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
    {
        if(u2!=null) {
            stopPlaying();
            mp = MediaPlayer.create(context, u2);
            mp.start();
            Toast.makeText(context, u2.toString(), Toast.LENGTH_SHORT).show();
        }
    }
    else if (action.equals(Intent.ACTION_SCREEN_OFF) && am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
    {
        if(u!=null) {
            stopPlaying();
            mp = MediaPlayer.create(context, u);
            mp.start();
            Toast.makeText(context, u.toString(), Toast.LENGTH_SHORT).show();

        }
    }

}

}

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

@Override
@SuppressWarnings("deprecation")
public void onCreate() {

    //Start listening for the Screen On, Screen Off, and Boot completed actions
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_BOOT_COMPLETED);
    //Set up a receiver to listen for the Intents in this Service
    receiver = new LockScreenReceiver();
    registerReceiver(receiver, filter);
    registerReceiver( receiver, new IntentFilter( "my.action" ) );
    registerReceiver( receiver, new IntentFilter( "my.action.unlock" ) );

    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Override
public void onDestroy() {
    unregisterReceiver(receiver);
    stopPlaying();
    super.onDestroy();
}

private void stopPlaying() {
    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
    }
}
}

And here's my Manifest

AndroidManifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_SERVICE"/>

    <service android:name=".LockScreenService" />

    <receiver
        android:name=".LockScreenService$LockScreenReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

What I want to do is, when user booted their phone, I want the receiver to be able to play the media player. So that an audio will play whenever user turn on or turn off their phone screen without needing to set it again manually through the app.

I don't know what's causing my app to crash. Is there something that I'm missing?

2 Answers2

1

Create a standalone BroadcastReceiver. Your service is not started at this time. So you are accessing Broadcast reciever through a service which actually doesn't exists. I think, thus creating a seperate class for BroadcastReceiver will do the trick.

DGN
  • 702
  • 3
  • 12
  • I followed your suggestion and the app doesn't crash anymore. But nothing seems to happen? – Pierson Leo Nov 27 '15 at 06:32
  • ok. Can you please edit your question with new code, so that we can check? – DGN Nov 27 '15 at 10:02
  • I made a new question post: https://stackoverflow.com/questions/33953483/broadcastreceiver-is-not-started-after-device-boot?noredirect=1#comment55666038_33953483 – Pierson Leo Nov 27 '15 at 10:47
-1

You Broadcast receiver name and android manifest receiver name is totally differ.

LockScreenReceiver or in manifest it is LockScreenService$LockScreenReceiver.

you have to manage it with different class of broadcastReciver.

Vishal Patel
  • 2,931
  • 3
  • 24
  • 60