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:
MainActivity.java
My IntentService class "myService.java":
AndroidManifest.xml
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>