android-BroadcastReceiver设备启动后未启动

android-BroadcastReceiver设备启动后未启动,第1张

概述设备重新启动后,我想运行接收器,以便设备打开或关闭设备时可以播放音频,而无需通过应用程序手动设置它们.MusicList.javaprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.music_layout);startService(new

设备重新启动后,我想运行接收器,以便设备打开或关闭设备时可以播放音频,而无需通过应用程序手动设置它们.

MusicList.java

protected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.music_layout);startService(new Intent(getApplicationContext(), LockScreenService.class));//other codes});//send chosen musiclv.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();        }    }});

这是我在接收器中写的

 public class LockScreenReceiver extends broadcastReceiver { MediaPlayer mp; ArrayList<file> mySongs; ArrayList<file> mySongs2; Uri u; Uri u2; AudioManager am;     @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);          //I trIEd this just for testing, but no toast is shown after device reboot             Toast.makeText(context, u2.toString(), Toast.LENGTH_SHORT).show();         }         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();             }         }     }private voID stopPlaying() {    if (mp != null) {        mp.stop();        mp.release();        mp = null;    }}}

这是服务类

public class LockScreenService extends Service {broadcastReceiver receiver;@OverrIDepublic 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" ) );   //Toast is not shown after device reboot   Toast.makeText(getApplicationContext(), "Starting service Now", Toast.LENGTH_SHORT).show();    super.onCreate();}@OverrIDepublic voID onDestroy() {    super.onDestroy();    unregisterReceiver(receiver);}}

我的清单,我相信这没有错

<uses-permission androID:name="androID.permission.READ_EXTERNAL_STORAGE"/><uses-permission androID:name="androID.permission.WAKE_LOCK" /><uses-permission androID:name="androID.permission.RECEIVE_BOOT_COMPLETED" /><uses-permission androID:name="androID.permission.READ_PHONE_STATE"/><uses-permission androID:name="androID.permission.SYstem_ALERT_WINDOW"/><uses-permission androID:name="androID.permission.ACCESS_BACKGROUND_SERVICE"/>    <service androID:name=".LockScreenService" />    <receiver        androID:name=".LockScreenReceiver">        <uses-permission androID:name="androID.permission.RECEIVE_BOOT_COMPLETED" />        <intent-filter>            <action androID:name="androID.intent.action.BOOT_COMPLETED" />            <action androID:name="my.action" />            <action androID:name="my.action.unlock" />        </intent-filter>    </receiver>

我做错了什么?有人说我应该在服务上做我的工作,但是那样做会使应用程序在启动时崩溃.我已经被困在这里好几天了.请帮忙.

另外,可能是一个相关的问题:我是否需要我的应用程序的sharedpreferences,以便它能够执行我想要的 *** 作?从设备存储中获取的文件.

这是我的源文件的链接,以便人们可以查明我做错了什么
https://github.com/PiersonLeo94/SOUN-WAKE

请看一下v1.1版本

解决方法:

这样定义清单.

<manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"    package="com.dev.hb.testing">    <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission androID:name="androID.permission.RECORD_AUdio" />    <uses-permission androID:name="androID.permission.CAMERA" />    <uses-permission androID:name="androID.permission.RECEIVE_BOOT_COMPLETED" />    <application        androID:allowBackup="true"        androID:icon="@mipmap/ic_launcher"        androID:label="@string/app_name"        androID:supportsRtl="true"        androID:theme="@style/Apptheme">        <receiver androID:name=".LockScreenReceiver">            <intent-filter>                <action androID:name="androID.intent.action.BOOT_COMPLETED" />                <action androID:name="androID.intent.action.QUICKBOOT_POWERON" />            </intent-filter>        </receiver>        <activity androID:name=".MainActivity">            <intent-filter>                <action androID:name="androID.intent.action.MAIN" />                <category androID:name="androID.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity androID:name=".WelcomeActivity" />    </application></manifest>
总结

以上是内存溢出为你收集整理的android-BroadcastReceiver设备启动后未启动全部内容,希望文章能够帮你解决android-BroadcastReceiver设备启动后未启动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1093011.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存