-
-
Save daviddoria/80c9c5dc98913b54bf00 to your computer and use it in GitHub Desktop.
Dynamically registered BroadcastReceiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.broadcastreceivertest; | |
import android.app.Activity; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.content.res.Configuration; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.Toast; | |
// http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html#broadcastreceiver | |
public class MainActivity extends Activity | |
{ | |
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() | |
{ | |
@Override | |
public void onReceive(Context c, Intent i) | |
{ | |
Log.i("onReceive", "onReceive"); | |
} | |
}; | |
private IntentFilter intentFilter = new IntentFilter( | |
Intent.ACTION_CONFIGURATION_CHANGED); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
protected void onResume() | |
{ | |
Log.i("onResume", "onResume"); | |
// this.registerReceiver(mBroadcastReceiver, intentFilter); | |
registerReceiver(mBroadcastReceiver, intentFilter); | |
super.onResume(); | |
} | |
@Override | |
protected void onPause() | |
{ | |
Log.i("onPause", "onPause"); | |
// this.unregisterReceiver(mBroadcastReceiver); | |
unregisterReceiver(mBroadcastReceiver); | |
super.onPause(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment