• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to send a broadcast intent from service

DigHeui

Lurker
Aug 19, 2015
2
0
I'm writing a simple app, my app will update volume number to a TextView when I increase or decrease the volume. So I think IntentService can help me.
I registered a BroadcastReceiver and start my IntentService in Activity. In onHandleIntent() method I create an Intent and use sendBroadcast() method to send this Intent. when I run app on virtual device, this app always crash, but when I remove sendBroadcast() method this App run well.
I dont' know why my app always crash. Please help me. Sorry for my bad english.

actvity_main.xml:
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.broadcastreceivertest2.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvVolume"
        android:text="12"
        android:textSize="30sp"
        android:layout_marginTop="20dp"
        android:inputType="text"
        android:layout_centerHorizontal="true"/>
  
</RelativeLayout>

MainActivity.java
Java:
public class MainActivity extends Activity {
    BroadcastReceiver myReceiver;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
            tv = (TextView)findViewById(R.id.tvVolume);
          
            myReceiver = new BroadcastReceiver() {
              
                @Override
                public void onReceive(Context context, Intent intent) {
                    // TODO Auto-generated method stub
                    tv.setText(intent.getExtras().getInt(myService.EXTRA_CHANGE_VOLUME));
                  
                }
            };
    }
  
    public void startMyService(){
        Intent myintent = new Intent(this, myService.class);
        startService(myintent);
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        registerReceiver(myReceiver, new IntentFilter(myService.ACTION_CHANGE_VOLUME));
        startMyService();
    }
@Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
      
        unregisterReceiver(myReceiver);
    }
}

My IntentService class "myService.java":
Java:
public class myService extends IntentService{
public static final String ACTION_CHANGE_VOLUME = "com.example.broadcastreceivertest2.CHANGE_VOLUME";
    public static final String EXTRA_CHANGE_VOLUME = "com.example.broadcastreceivertest2.VOLUME";
    public myService() {
        super("myService");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub
        AudioManager manager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
        int volumeSystem = manager.getStreamVolume(AudioManager.STREAM_SYSTEM);
        Intent myIntent = new Intent();
        myIntent.setAction(ACTION_CHANGE_VOLUME);
        myIntent.putExtra(EXTRA_CHANGE_VOLUME, volumeSystem);
        getApplicationContext().sendBroadcast(myIntent); //this app crash when call sendBroadcast() method
    }
  
}

AndroidManifest.xml
Java:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceivertest2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
      
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.example.broadcastreceivertest2.myService"
            android:exported="false"/>
         
    </application>

</manifest>
 
Because I want when I press the volume button on smart phone in order to increase or decrease the volume and the app will update automatically the volume number on a textview :)
sure you can achieve this in your activity by Overriding onKeyDown Method to add a listener to the volume key ,then you can do whatever you want when the user increase or decrease the volume , including showing the volume in a textview
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones