Android 当手机';s启动

Android 当手机';s启动,android,Android,下面是我如何编写我的应用程序 音乐学家 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.music_layout); startService(new Intent(getApplicationContext(), LockScreenService.class)); //other

下面是我如何编写我的应用程序

音乐学家

 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();
            }
        }
    });
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.music_layout);
startService(新意图(getApplicationContext(),LockScreenService.class));
//其他代码
});
//发送选定的音乐
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
if(lockIsChosen!=null){
//其他代码
试一试{
意向i=新意向(“我的行动”);
i、 普通话(“posLock”,newPosition2)。普通话(“songlistLock”,mySongs)。普通话(“lockSound”,“lock”);
发送广播(一);
}捕获(例外e){
Log.e(标记“意图错误”);
}
完成();
}
if(unlockIsChosen!=null){
//其他代码
试一试{
意图i=新意图(“my.action.unlock”);
i、 putExtra(“posUnlock”,newPosition3)。putExtra(“songlistUnlock”,mySongs)。putExtra(“unlockSound”,“unlock”);
发送广播(一);
}捕获(例外e){
日志e(标签“意图错误2”);
}
完成();
}
}
});
在我的服务课上,这是我写的

锁屏服务

 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;
    }
}
}
公共类锁屏服务扩展服务{
MediaPlayer mp;
ArrayList mySongs;
ArrayList mySongs2;
乌里乌;
uriu2;
音频经理am;
广播接收机;
公共类LockScreenReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
String action=intent.getAction();
am=(AudioManager)context.getSystemService(context.AUDIO\u服务);
if(action.equals(Intent.action\u BOOT\u COMPLETED)){
意向服务=新意向(上下文,LockScreenService.class);
启动服务(服务);
}
if(action.equals(“my.action”)){
Bundle b=intent.getExtras();
mySongs=(ArrayList)b.getParcelableArrayList(“songlistLock”);
int位置=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位置=b.getInt(“posUnlock”,0);
u2=Uri.parse(mySongs2.get(position.toString());
}
if(action.equals(Intent.action屏幕打开)和&am.getRingerMode()==AudioManager.RINGER模式正常)
{
如果(u2!=null){
停止铺设();
mp=MediaPlayer.create(上下文,u2);
mp.start();
Toast.makeText(上下文,u2.toString(),Toast.LENGTH_SHORT.show();
}
}
else if(action.equals(Intent.action\u SCREEN\u OFF)和&am.getRingerMode()==AudioManager.RINGER\u MODE\u NORMAL)
{
如果(u!=null){
停止铺设();
mp=MediaPlayer.create(上下文,u);
mp.start();
Toast.makeText(上下文,u.toString(),Toast.LENGTH_SHORT).show();
}
}
}
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
@抑制警告(“弃用”)
public void onCreate(){
//开始监听屏幕打开、屏幕关闭和引导完成的操作
IntentFilter过滤器=新IntentFilter(Intent.ACTION屏幕打开);
filter.addAction(Intent.ACTION\u SCREEN\u OFF);
filter.addAction(Intent.ACTION\u BOOT\u完成);
//设置接收器以侦听此服务中的意图
接收器=新的锁屏接收器();
寄存器接收器(接收器、过滤器);
registerReceiver(新意向过滤器的接收者(“我的行动”);
registerReceiver(接收者,新意图过滤器(“my.action.unlock”);
super.onCreate();
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
返回开始时间;
}
@凌驾
公共空间{
未注册接收人(接收人);
停止铺设();
super.ondestory();
}
私人住宅{
如果(mp!=null){
mp.stop();
mp.release();
mp=null;
}
}
}
这是我的舱单

雄激素单

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

我想做的是,当用户启动他们的手机时,我希望接收器能够播放媒体播放器。这样,无论用户何时打开或关闭手机屏幕,都可以播放音频,而无需通过应用程序手动重新设置


我不知道是什么原因导致我的应用程序崩溃。有什么我遗漏的吗?

您广播的接收器名称和android manifest接收器名称完全不同

LockScreenReceiver
或在清单中它是
LockScreenService$LockScreenReceiver


您必须使用不同类别的BroadcastReceiver来管理它。

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

在单独的类中发布您的错误日志take BroadcastReceiver。它正在崩溃,因为当android试图在服务尚未启动时启动广播时,它无法找到它,这就是它崩溃的原因。我按照你的建议,应用程序不再崩溃。但似乎什么都没发生?好的。你能用新代码编辑你的问题吗?这样我们就可以检查了。我发了一个新的问题帖子: