I'v already read post about:
BroadcastReceiver myReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// do whatever you want to do
}
};
registerReceiver(myReceiver, new IntentFilter("ACTION"));
unregisterReceiver(myReceiver);
Intents:
ACTION_PACKAGE_INSTALL
ACTION_PACKAGE_REPLACED
ACTION_PACKAGE_CHANGED
ACTION_PACKAGE_ADDED
How to do subject or when installation failed
Related
I want to update widget manually. So, I add in an AndroidManifest filter:
<action android:name="com.myapp.WIDGET_UPDATE"/>
And I try to send broadcasts from adb console like this
am broadcast -a com.myapp.WIDGET_UPDATE
But nothing happens. Here is the code from onReceive:
public class myapp extends AppWidgetProvider {
private final static IntentFilter intentFilter = new IntentFilter("com.myapp.WIDGET_UPDATE");
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
updateWidget(context);
}
};
}
you have to put one button in layout_wid.xml file.
in onUpdate() you have to find the id of your button.and put click event to refresh your widget
Intent imageview2Intent= new Intent(context, WatchWidget.class);
PendingIntent pendingIntent= PendingIntent.getBroadcast(context,0, imageview2Intent,0);
remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent);
you try this way.
I am new to Android development.
I want to get a notice when the operating system (because of lack of memory) or other application (Task killer e.g. ZDbox) restart other applicatoin (not mine).
I tried BroadcastReceiver. It did not get any Intent when an app was killed and the user started it manually. I have used PACKAGE_RESTARTED intent but BroadcastReceiver did not get any Intent.
Any suggestions?
public class MyService extends Service {
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
filter.addDataScheme("package");
BroadcastReceiver pkgRemoveReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// do my stuff
}
};
registerReceiver(pkgRemoveReceiver, filter);
}
This question already has answers here:
Having a Service receive SMS messages
(4 answers)
Closed 7 months ago.
I have a SmsReceiver class that I want to register in the main activity, what exactly should I do ?
I am new to Android.
Either you can do 2 things:
Create and define BroadcastReceiver in the Manifest
Create and register the BroadcastReceiver in code.
For option 2 (which you are asking):
Create a BroadcastReceiver in code (MyBroadcastReceiver).
Declare MyBroadcastReceiver in the scope of your Activity:
MyBroadcastReceiver mMyBroadcastReceiver;
Register the BroadcastReceiver in your Activity by:
IntentFilter filter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
this.registerReceiver(mMyBroadcastReceiver, filter);
For API level 19 and above
IntentFilter smsFilter = new IntentFilter();
smsFilter.addAction(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(this.receiver, filter);
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//some action
}
};
IntentFilter filter = new IntentFilter("YOUR_ACTION");
registerReceiver(receiver, filter);
Hope this helps
As you've asked to register BroadcastReceiver inside your MainActivity that fits to registerReceiver-codewise-inside-MainActivity
Here's what code looks like ==>
public class MainActivity extends Activity
{
BroadcastReceiver smsReceiver;
private IntentFilter myFilter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
registerReceiver(smsReceiver, myFilter); // register BroadCastReceiver at time of initialization of Activity with proper Intent-Filter
}
#Override
protected void onPause()
{
unregisterReceiver(smsReceiver); // UnRegister BroadCastReceiver as you no longer have your activity at Foreground -- Saving CPU & Battery Drainage
super.onPause();
}
#Override
protected void onResume()
{
registerReceiver(smsReceiver, myFilter); // Register BroadCastReceiver Once again as your activity comes from pause to forground state again.
super.onResume();
}
smsReceiver = new BroadcastReceiver() //Implementation of your BroadCastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
// Do whatever you like as sms is received and caught by these BroadCastReceiver
Toast.makeText(context, "SMS Received", Toast.LENGTH_LONG).show();
}
};
}
I'm stuck here at my previous struggle >> Prev. Struggle!
Raanan there helped! me a lot but then he I think went away as timing zone is different , now I'm stuck with my service code that I'm using to call my BroadcastReceiver() that is in the activity! and also I'm not getting with what parameter I should load the filter.addAction(action); in place of action??
Kinldy guide me!
CODE in the Server:
Toast.makeText(Server.this, hr +" , " +min, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, andRHOME.class);
//intent.putExtra("sendMessage","1");
sendBroadcast(intent);
and CODE IN THE ACITIVITY(Broadcast Receiver)
private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "IN DA BroadCASTER",
Toast.LENGTH_LONG).show();
sendMessage("1");
}
};
IntentFilter filter = new IntentFilter();
You need to add these line to regiester your receiver for some action for example define a Global variable like this:
public static String NOTIFCATION_BROADCAST_ACTION = "com.your_packagename.UPDATE_NOTIFICATION_INTENT";
then register the action like this in your activity onCreate() Method.
IntentFilter filter = new IntentFilter();
filter.addAction(Global.NOTIFCATION_BROADCAST_ACTION);
registerReceiver(ReceivefrmSERVICE, filter);
Then send the broadcast from your service like this
Intent broadcast = new Intent();
broadcast.setAction(Global.NOTIFCATION_BROADCAST_ACTION);
sendBroadcast(broadcast);
Then in your broadcast Receiver filter this action like this
private BroadcastReceiver ReceivefrmSERVICE = new BroadcastReceiver(){
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Global.NOTIFCATION_BROADCAST_ACTION)) {
//Do your stuff here :)
}
}
};
I am listening to both connecting and disonnecting the power for my galaxy.
I have creatd 2 BroadCastReceivers, one for connect, and one for disconnect.
When I try to implement, I only get the connected data, even when disconnecting the power cable.
The intent is sent, but looks like it's the wrong one.
Here is the activity code:
// Handle Power On
PowerConnectedBCReceiver myPowerConnectedBCReceiver = new PowerConnectedBCReceiver();
IntentFilter intentPowerOnFilter = new IntentFilter();
intentPowerOnFilter.addAction("android.intent.action.ACTION_POWER_CONNECTED");
registerReceiver(myPowerConnectedBCReceiver, intentPowerOnFilter);
BroadcastReceiver PowerConnectedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
showToast("connected");
}
};
// Handle Power Off
PowerConnectedBCReceiver myPowerDisonnectedBCReceiver = new PowerConnectedBCReceiver();
IntentFilter intentPowerDisconnectedOnFilter = new IntentFilter();
intentPowerDisconnectedOnFilter.addAction("android.intent.action.ACTION_POWER_DISCONNECTED");
registerReceiver(myPowerDisonnectedBCReceiver, intentPowerDisconnectedOnFilter);
BroadcastReceiver PowerDisconnectedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
showToast("disconnected");
}
};
Registering both BC to do the work
registerReceiver(PowerDisconnectedReceiver, new IntentFilter("com.neglected.POWER_DISCONNECTED"));
registerReceiver(PowerConnectedReceiver, new IntentFilter("com.neglected.POWER_CONNECTED"));
BroadCast connected code:
public class PowerConnectedBCReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Phone was connected to power" , Toast.LENGTH_LONG).show();
Intent tIntent = new Intent("com.neglected.POWER_CONNECTED");
context.sendBroadcast(tIntent);
}
}
Broadcast disconnected code:
public class PowerDisconnectedBCReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Phone was disconnected from power" , Toast.LENGTH_LONG).show();
Intent tIntent = new Intent("com.neglected.POWER_DISCONNECTED");
context.sendBroadcast(tIntent);
}
}
IS the code wrong?
Can I listen to both actions? seperately?
I can't see extra been sent with the CONNNECTED Action, is there?
Not sure what your last two classes (*BCReeivers) are supposed to be doing. Your first block of code looks ok. It will be limited to the lifecycle of the enclosing Activity if that matters.
For the Galaxy S, you may not be able to rely on those Intents. In particular, I have found that the Verizon Fascinate (their version of the Galaxy S) to be very buggy. See here: http://devblog.bu.mp/how-to-ddos-yourself
There was an error in my Broadcast instantiation. I mistakenly used the PowerConnectedBCReceiver instead of PowerDisconnectedBCReceiver
problem solved.