继续互相呼叫服务/广播

时间:2018-05-29 22:42:17

标签: android kotlin

我正在构建一个简单的应用程序,它会持续监控媒体级别,并将其调整为最高级别的20%,如果用户增加,则应该再次回到20%。

我遵循的概念是通过服务进行监控过程,一旦该服务被破坏,它就会调用广播接收器,广播接收器又会再次调用接收器,依此类推,作为无限循环,但看起来有些不对劲下面的代码,soit没有按预期工作,服务/广播不会一直呼叫对方!

我开始mainActivity作为:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val audio = this.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        val level = audio.getStreamVolume(AudioManager.STREAM_MUSIC)

        val maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        val percent = 0.2f
        val twintyVolume = (maxVolume * percent).toInt()

        if ( level > twintyVolume) {
            Toast.makeText(this,"audio level is $level", Toast.LENGTH_LONG).show()
            audio.setStreamVolume(AudioManager.STREAM_MUSIC,twintyVolume,0)
        }

        this.startService(Intent(this, VolumeCheck::class.java))
    }
}

以上进行初步检查并将媒体音量减小到最大音量的20%,然后启动服务,这与下面的代码相同:

class VolumeCheck : Service() {

    private lateinit var context: Context

    override fun onCreate() {
        super.onCreate()
        context = this
        Toast.makeText(this, "service created", Toast.LENGTH_SHORT).show();
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        super.onStartCommand(intent, flags, startId)
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

        val audio = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        val mediaPlayer = MediaPlayer()


     //   Thread().run {
            val maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
            val percent = 0.2f
            val twintyVolume = (maxVolume * percent).toInt()
            if (mediaPlayer.isPlaying) {
                val level = audio.getStreamVolume(AudioManager.STREAM_MUSIC)
                if ( level > twintyVolume) {
                    Toast.makeText(context,"audio level is $level", Toast.LENGTH_LONG).show()
                    audio.setStreamVolume(AudioManager.STREAM_MUSIC,twintyVolume,0)
                }
            }
      //      Thread.sleep(3000)
     //   }
        stopSelf()
        return Service.START_STICKY
    }

    override fun onBind(intent: Intent): IBinder? {
        //TODO for communication return IBinder implementation
        return null
    }

    override fun onDestroy() {
        super.onDestroy()
        Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show()
        val intent = Intent("com.kortex.mediafix.BootUpReceiver")
        sendBroadcast(intent)
    }
}

一旦服务被销毁,它就会调用引导广播接收器,然后再接收服务:

class BootUpReceiver: BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        context.startService(Intent(context, VolumeCheck::class.java))
    }
}

Manifest是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kortex.mediafix">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".BootUpReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
        <service android:name=".VolumeCheck" />
    </application>

</manifest>

3 个答案:

答案 0 :(得分:0)

val intent = Intent("com.kortex.mediafix.BootUpReceiver")

此行不会调用您的广播接收器,而是使用意图操作制作意图&#34; com.kortex.mediafix.BootUpReceiver&#34;

更改清单中的BootUpReceiver条目以接收此操作

<receiver android:name=".BootUpReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <action android:name="com.kortex.mediafix.BootUpReceiver" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

答案 1 :(得分:0)

我使用从ContentObserver调用的service解决了这个问题,所以我的代码现在是:

  1. MainActivity启动应用,并进行首次调整,然后启动服务:

    class MainActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        val audio = this.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        val level = audio.getStreamVolume(AudioManager.STREAM_MUSIC)
    
        val maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        val percent = 0.2f
        val twintyVolume = (maxVolume * percent).toInt()
    
        if ( level > twintyVolume) audio.setStreamVolume(AudioManager.STREAM_MUSIC,twintyVolume,0)
    
        Toast.makeText(this,"Audio level adjusted to 20% instead of $level", Toast.LENGTH_LONG).show()
    
        this.startService(Intent(this, VolumeCheck::class.java))
    
        finish()
       }
    }
    
  2. Broadcast每次重启设备时启动应用和服务:

    class BootUpReceiver: BroadcastReceiver() {
    
    override fun onReceive(context: Context, intent: Intent) {
        context.startService(Intent(context, VolumeCheck::class.java))
       }
    }
    
  3. Service注册并致电观察员:

    class VolumeCheck : Service() {
    
    private lateinit var context: Context
    private lateinit var myObserver: VolumeOnserver
    
    
    override fun onCreate() {
        super.onCreate()
        context = this
        // Define the VolumeOnserver
        myObserver = VolumeOnserver(context, Handler())
        Toast.makeText(this, "service created", Toast.LENGTH_SHORT).show();
    }
    
    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        super.onStartCommand(intent, flags, startId)
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
    
        // Register the VolumeOnserver for setting changes
        contentResolver.registerContentObserver(android.provider.Settings.System.CONTENT_URI ,true, myObserver);
    
        return Service.START_STICKY
    }
    
    override fun onBind(intent: Intent): IBinder? {
        return null
    }
    
    override fun onDestroy() {
        super.onDestroy()
    
        // Unregister the VolumeOnserver
        contentResolver.unregisterContentObserver(myObserver);
       }
    }
    
  4. Observer观察settings中的任何更改并检查媒体音量并根据需要进行调整:

    class VolumeOnserver (context: Context, h: Handler?): ContentObserver(h) {
    
    private val context = context
    
    override fun onChange(selfChange: Boolean) {
        onChange(selfChange, uri = null)
    }
    
    // Implement the onChange(boolean, Uri) method to take advantage of the new Uri argument.
    override fun onChange(selfChange: Boolean, uri: Uri?) {
        // Handle change.
        val audio = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        val level = audio.getStreamVolume(AudioManager.STREAM_MUSIC)
    
        val maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        val percent = 0.2f
        val twintyVolume = (maxVolume * percent).toInt()
    
        if ( level > twintyVolume) audio.setStreamVolume(AudioManager.STREAM_MUSIC,twintyVolume,0)
    
        // Toast.makeText(context,"audio level is $level", Toast.LENGTH_LONG).show()
       }
    }
    
  5. Manifest文件是:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.kortex.mediafix">
    
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <receiver android:name=".BootUpReceiver"
                android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                   <action android:name="com.kortex.mediafix.BootUpReceiver" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </receiver>
            <service android:name=".VolumeCheck" />
        </application>
    
    </manifest>
    

    我正在运行的应用是here

    但有一个问题是它不稳定,我用户坚持要改变音量,经过多次试验后,服务和/或观察者不再工作,直到应用程序重新启动。

答案 2 :(得分:0)

另一种方法(不Service}是从ContentObserver

致电BroadcastReceiver
  1. BroadcastReceiver

    class BootUpReceiver:BroadcastReceiver(){

    override fun onReceive(context: Context, intent: Intent) {
        val myObserver = VolumeOnserver(context, Handler())
    
        // Register the VolumeOnserver for setting changes
        context.contentResolver.registerContentObserver(
                android.provider.Settings.System.CONTENT_URI ,true,
                myObserver)
       }
    }
    
  2. ContentObserver

    类VolumeOnserver(context:Context,h:Handler?):ContentObserver(h){

    private val context = context
    
    override fun onChange(selfChange: Boolean) {
        onChange(selfChange, uri = null)
    }
    
    // Implement the onChange(boolean, Uri) method to take advantage of the new Uri argument.
    override fun onChange(selfChange: Boolean, uri: Uri?) {
        // Handle change.
        val audio = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        val level = audio.getStreamVolume(AudioManager.STREAM_MUSIC)
    
        val maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        val percent = 0.2f
        val twintyVolume = (maxVolume * percent).toInt()
    
        if ( level > twintyVolume) audio.setStreamVolume(AudioManager.STREAM_MUSIC,twintyVolume,0)
        }
    }
    
  3. MainActivity

    类MainActivity:AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        val audio = this.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        val level = audio.getStreamVolume(AudioManager.STREAM_MUSIC)
    
        val maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        val percent = 0.2f
        val twintyVolume = (maxVolume * percent).toInt()
    
        if ( level > twintyVolume) audio.setStreamVolume(AudioManager.STREAM_MUSIC,twintyVolume,0)
    
        finish()
       }
    }