BroadcastReceiver doesn't work - android

I added receiver in manifest
<receiver android:name=".PackageReceiver"
android:enabled="true">
<intent-filter android:priority="100">
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
And this is my BroadcastReceiver
public class PackageReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Log.e("noti", "sucess");
//Start Notification Service
Intent i = new Intent(context, NotificationService.class);
context.startService(i);
}}
..
When I install this package, It's didn't work...
This package don't have activity.(only service and resource)
Is this a problem?
Then..
How to call BroadcastReceiver in this package without activity?

This is a question that is asked a lot.
Check out this Commonsware blog.
Starting with 3.1 when applications are installed they are in a “stopped” state so they will not be able to run until the user explicitly launches them. Pressing Force Stop will return them to this state.
There needs to be an active component in your application that the user can start. When your activity gets started, your BroadcastReceiver will be registered.

You have just declared and implemented a BroadcastReceiver, but you haven't started it yet, you need an entry point to start your receiver ( activity or service )
here is the code to register
PackageReceiver packageReceiver= new PackageReceiver(this);
registerReceiver( packageReceiver, new IntentFilter("android.intent.action.PACKAGE_ADDED"));

Related

Broadcast Receiver is not working properly

I am experimenting with broadcast receiver in android and I have this class as broadcast receiver:
public class BroadcastInbuilt extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Battery Low, Please Charge!", Toast.LENGTH_LONG).show();
Log.i("BroadcastPersonal","Happening");
MainActivity.b1.setText("Change text if working!!");
}
}
I have added this in Android Manifest inside application tag:
<receiver
android:name=".BroadcastInbuilt"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
There is no code in my main activity. When I change my battery level to low the toast does not appear. i am following the android tutorial and I have done exactly same as told here
https://www.youtube.com/watch?v=Uf4V5OSJji8&list=PLlyCyjh2pUe9wv-hU4my-Nen_SvXIzxGB&index=44
Result is at 9:40 in video. Now his app shows toast but mine does not.
I did some research and found out that you should not register your receiver in Android Manifest but register them dynamically through programming.
BroadcastReceiver myBroadcast = new BroadcastInbuilt(); // name of class which extends BroadcastReceiver
IntentFilter filter =new IntentFilter(BatteryManager.EXTRA_BATTERY_LOW);
filter.addAction(Intent.ACTION_BATTERY_LOW);
this.registerReceiver(myBroadcast, filter);
Be sure to unregister broadcast in onDestroy()
unregisterReceiver(myBroadcast);
Note: if receiver was registered in onCreate() then it should be unregistered in onDestroy().If it was registered in onResume() then it should be unregistered in onPause().

how to make my service work when user open the mobile

I want to make my service always working but in the normal service if the user close the phone and open it or restart it the service is stoping can you help me. thanks
You could use a Broadcast Receiver that would take the permission to broadcast a message on the restart of the phone that would tell the service to be started or as we call it in technical terms we would use an intent-filter having the action of starting the service when in the actions (or the ) the boot process is completed.
In manifest file :-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In application tag of manifest.xml :-
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In MyBroadcastReceiver.java :-
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
Now, at the end the service class of MyService would be started by this boradcasting.

Broadcast Receiver not getting triggered

This should be fairly easy but I somehow can't get a Broadcast receiver's onReceive method triggered. Details below:
App B provides a broadcast receiver.
Manifest:
<receiver android:name=".MyNotificationReceiver">
<intent-filter>
<action android:name="com.go.foo.A_ACTION" />
</intent-filter>
</receiver>
Java:
public class MyNotificationReceiver extends BroadcastReceiver {
private final String TAG= "MyNotificationReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "this is not shown" , Toast.LENGTH_LONG).show();
}
}
App A is Broadcast Sender App:
Java
Intent intent = new Intent();
intent.setAction("com.go.foo.A_ACTION");
sendBroadcast(intent);
Log.d(TAG, "broadcast intent sent...");
I can see the log statement that the broadcast is sent but the receiver's onReceive() callback is not getting triggered. Am I doing something wrong?
Interesting why it's not working. Try this one out. I know the default values of exported and enabled are true. But still have a try at it.
<receiver android:name=".MyNotificationReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="com.go.foo.A_ACTION" />
</intent-filter>
</receiver>
Learned something new today. As of Android 4.1, the system no longer sends broadcasts to app components without an activity. The broadcast received started working after I added an activity in the manifest.

How to launch an android service from a custom call number

I'm developing an Android application, i have a service running in background that i want to lauch it by introducing a custom ussd number. For example when i call #12345# my service starts.Thank you in advance
You can register a BroadcastReceiver in your manifest that listens for android.provider.Telephony.SECRET_CODE intents. You also specify the secret code in the manifest.
For example, this manifest entry registers MyBroadcastReceiver for the secret code *#*#12345#*#*.
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE"/>
<data android:scheme="android_secret_code" android:host="12345"/>
</intent-filter>
</receiver>
MyBroadcastReceiver should look something like this:
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
// Create an intent to start your Service.
}
}

Start application in background on bootup

I would like to start my application when the phone boots up. But the user should not know that the application has started. I am doing the boot up by receiving the BOOT_COMPLETE event in a broadcast receiver and am able to minimize the application by using moveTaskToBack(true).
But my application is visible for a split second before it gets minimized and I do not want this to happen. My application does not have any services. Is there any way in which I can start the application minimized without the end user noticing ?
Thanks
enter code here
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}

Categories

Resources