I want to set a broadcast receiver to run some function when it gets the broadcast message, in this example, I want to catch the download's manager intent:
DownloadManager.ACTION_DOWNLOAD_COMPLETE
I looked at the Android API examples and haven't found a way to do this
You should read this first:
http://developer.android.com/reference/android/content/BroadcastReceiver.html
Then look here for examples.
Android Samples: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/appwidget/ExampleBroadcastReceiver.html
Blog post: http://www.androidcompetencycenter.com/2009/01/basics-of-android-part-ii-intent-receivers/
Try this:
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE) ){
// do something
}
}
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
You can create a class that inherits from BroadcastReceiver:
public class MyDownloadCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
}
}
And then register this class in your application manifest like so:
<receiver android:enabled="true"
android:name="MyDownloadCompleteReceiver"
android:label="downloadCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
Related
I have an application that use parse push notification service. Here is the class that I'm using for receiving notification:
public class MessageReceiver extends ParsePushBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//...some code in here
}
And I also register this custom receiver in my manifest:
<receiver
android:name="com.package.MessageReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
Everything is fine with notification system. After a notification have received, I want to update the running activity UI accordingly (like showing an icon for new notification) but I don't have access to the activity object in onReceive method. What is the best practice to do that? I couldn't use the context object in this matter.
Thanks
I believe you can accomplish this by using a BroadcastReceiver.
You would define the receiver in the activities you want to have access to,
See snippet below.
public MyActivity extends Activity
{
//... code
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//... update ui here
}
};
#Override
protected void onResume() {
IntentFilter filter = new IntentFilter();
filter.addAction("suitablename");
registerReceiver(receiver, filter);
super.onResume();
}
#Override
protected void onPause() {
unregisterReceiver(receiver);
super.onPause();
}
//... code
}
Then in your custom receiver send the broadcast.
public class MessageReceiver extends ParsePushBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//...some code in here
Intent intent = new Intent();
intent.setAction("suitablename");
context.sendBroadcast(intent);
}
}
Don't forget to update the manifest
<activity
android:name=".MyActivity" >
<intent-filter>
<action android:name="suitablename"></action>
</intent-filter>
</activity>
I have an android application. Here whenever the user changes the device timezone, I want a event listener to notify me? How can I accomplish it?
Register a receiver with the following intent filters
filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
filter.addAction(Intent.ACTION_TIME_CHANGED);
and register it in the OnCreate of your activity
public void onCreate() {
super.onCreate();
registerReceiver(receiver, filter);
}
Create a Broadcast receiver as follows.
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_TIME_CHANGED) ||
action.equals(Intent.ACTION_TIMEZONE_CHANGED))
{
doWorkSon();
}
}
};
and unregister it in the onDestroy of your activity:
public void onDestroy() {
super.onDestroy();
unregisterReceiver(m_timeChangedReceiver);
}
You need to create a BroadcastReceiver for intent action ACTION_TIMEZONE_CHANGED. BroadcastReceiver is described here.
In manifest file
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.TIMEZONE_CHANGED"/>
</intent-filter>
</receiver>
In myReceiver class
override fun onReceive(context: Context, intent: Intent) {
var action : String? = intent.action
var timeZone: String? = intent.getStringExtra("time-zone")
Toast.makeText(context,action+timeZone,Toast.LENGTH_LONG).show()
Is there anyway to register a BroadcastReceiver in AndroidManifest.xml and receives broadcast that is send by a LocalBroadcastManager?
Currently I must call
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
to register a Receiver, declare in AndroidManifest.xml won't work. But this means I must know exactly the receiver's package name and class name, not just the intent filter. Is it possible to declare the receiver in the manifest file?
following is my current code.
AndroidManifest.xml:
...
<receiver
android:name="com.example.test.MessageReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="com.m2x.test.intent.MESSAGE_RECEIVED" />
</intent-filter>
</receiver>
...
MainActivity.java:
Intent intent = new Intent();
intent.setAction("com.m2x.test.intent.MESSAGE_RECEIVED");
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mContext.get());
manager.sendBroadcast(intent);
MessageReceiver.java
public class MessageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.m2x.test.intent.MESSAGE_RECEIVED")) {
Toast.makeText(context, "user message received", Toast.LENGTH_SHORT).show();
}
}
}
No, you can't.
The local BroadcastReceiver isn't a real BroadcastReceiver, basically its a list of callbacks functions.
You can check the source code of LocalBroadcastManager.java.
I'm starting with android and I'm having some lessons to learn some android concepts. In this case, I'm practicing with the BroadCast receivers.
I have to create a BroadCast Receiver that when I boot the phone/emulator, starts an activity which shows a plain text.
I have this class:
public class MainActivity extends Activity {
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
IntentFilter mfilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
registerReceiver(receiver, mfilter);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
But i'm not getting to do what I need, it simply does nothing, so... What I'm doing wrong here?
In the manifest I just have the activity declared.
Try this...
Step1:
set the permission in AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Step2:
Add this is intent filter in receiver,
<receiver android:name=".BootReciever">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
Step3:
Now you can start your application's first activity from onReceive method of Receiver class..
public class BootReciever extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(context, MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
You can not register ACTION_BOOT_COMPLETED receiver dynamically(it's not a way).
ACTION_BOOT_COMPLETED receiver has to be registered statically in manifest file
ex
<receiver android:name="com.myapp.receiver.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
When you register a receiver in Activity or in Service it's lifetime is bound with them
You need to define your receiver inside app manifest.
For ex:
Class
class MyClass extends BroadCastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
IntentFilter mfilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
registerReceiver(receiver, mfilter);
}
}
Create your own MyReceiver class by extending BroadcastReceiver class, and register your MyReceiver in the manifest with filter ACTION_BOOT_COMPLETED.
<receiver android:name="com.test.MyReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I have a BroadcastReceiver who should receive the BOOT.
Is there a simple way to check if the BroadcastReceiver receives the BOOT correctly?
Thanks
Assuming BOOT means BOOT_COMPLETED then you could use the following:
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
//Do a Log or, less likely, a Toast or start your application here.
}
}
};
You would register this in the manifest like such:
<receiver android:name="com.example.yourAppName.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>