手机启动时应用程序立即崩溃

时间:2015-11-27 05:28:42

标签: android

以下是我编写应用的方式。

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();
            }
        }
    });

在我的服务类中,这就是我写的

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;
    }
}
}

这是我的清单

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>

我想要做的是,当用户启动手机时,我希望接收器能够播放媒体播放器。因此,只要用户打开或关闭手机屏幕,音频就会播放,而无需通过应用程序再次手动设置。

我不知道导致我的应用崩溃的原因。有什么东西我不见了吗?

2 个答案:

答案 0 :(得分:1)

创建一个独立的BroadcastReceiver。您的服务目前尚未启动。因此,您通过实际不存在的服务访问广播接收器。我认为,因此为BroadcastReceiver创建一个单独的类就可以了。

答案 1 :(得分:-1)

您广播的接收者名称和Android清单接收者名称完全不同。

LockScreenReceiver或显示为LockScreenService$LockScreenReceiver

您必须使用different class broadcastReciver管理它。