So in fragment class, I have a AsyncTask class and there I am calling BroadcastReceiver at onPostExecute() method of AsyncTask -
Android Manifest -
<receiver
android:name="com.iddl.main.IncomingBBStream"
android:label="IncomingBBStream">
<action android:name="com.iddl.main.BBbroadcast" />
<intent-filter>
<action android:name="android.bluetooth.
device.action.ACL_DISCONNECTED" />
</intent-filter>
</receiver>
Fragment Class -
#Override
protected void onPostExecute(Void result)
{
Intent intent = new Intent();
intent.setAction("com.iddl.main.BBbroadcast");
intent.putExtra("uuid", uuid);
getActivity().sendBroadcast(intent);
}
BroadcastReceiver Class -
public class IncomingBBStream extends BroadcastReceiver {
public void onReceive(Context context, Intent intent)
{
Log.i(TAG, "onReceive BroadcastReceiver------");
}
It should at least print the log message but it not doing so.
NOTE:
I have to add intent.setClass(getActivity(), IncomingBBStream.class); to get it work but that doesn't make sense because I already passed the action "com.iddl.main.BBbroadcast" that matches with the manifest's receiver name "com.iddl.main.IncomingBBStream". So it knows which class to call.
you probably want to do that:
<receiver
android:name="com.iddl.main.IncomingBBStream"
android:label="IncomingBBStream">
<intent-filter>
<action android:name="com.iddl.main.BBbroadcast"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="android.bluetooth.
device.action.ACL_DISCONNECTED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
each intent filter separate on its own tag and always include the default category.
Related
with this How do I start my app on startup? post run app when devise power on success
but when devise run this show my main_activity
i want run app in background (Because i call a volley request with timer for check server in background )
my manifest.xml :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:enabled="true"
android:exported="true"
android:name="other_class.CLASS_START_UP"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
my class start_up :
public class CLASS_START_UP extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
i use this post Run volley request every 5 minutes in background android to run volley request in timer
my timer code is :
private Runnable runnableCode = new Runnable() {
#Override
public void run() {
get_start_info();
// Repeat this the same runnable code block again another 2 seconds
handler.postDelayed(runnableCode, 10000);
}
};
my problem is i dont want when devise turn on show main activity show to user (foreground) i want only run in background
i am sorry i am not good in English language
Try This one
<receiver android:name=".service.ShutdownReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.REBOOT"/>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
ShutdownReciver.java
public class ShutdownReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("Brodcast Call","####");
Toast.makeText(context, "service", Toast.LENGTH_SHORT).show();
// context.startService(new Intent(context,
PowerButtonService.class));
Intent myIntent = new Intent(context, PowerButtonService.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
I added a receiver to listen when app is installed. But it is not working. Here is my code in AndroidManifest.xml
<receiver android:enabled="true"
android:exported="true"
android:name="com.bsp.iqtest.reiceiver.IQTestReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
Here is my code in MainActivity (launcher activity) , function onCreate.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IQTestReceiver br = new IQTestReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addDataScheme("package");
registerReceiver(br, intentFilter);
}
Here is my code in IQTestReceiver (this class is written in other file)
public class IQTestReceiver extends BroadcastReceiver {
public IQTestReceiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
String packageName=intent.getData().getEncodedSchemeSpecificPart();
Log.e("HELLO",packageName);
}
}
I set a breakpoint in onReceive function , but it doesn't run when i debug.
Thanks for your helping.
You can not receive PACKAGE_ADDED or PACKAGE_REPLACED for your own app, if that is what you're trying.
"Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast."
See http://developer.android.com/reference/android/content/Intent.html
set your broadcasrt in manifest like this
<receiver
android:name=".IQTestReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="check_values"/>
</intent-filter>
</receiver>
and send the broadcast like this.....Intent it1=new Intent(Intent.ACTION_USER_PRESENT);
it1.setAction("check_values");
it1.putExtra("data_key1",message);
sendBroadcast(it1);
and in on receive would be like this....
#Override
public void onReceive(Context context, Intent intent)
{
data1=intent.getStringExtra("data_key1");
System.out.println("ffffff11" + data1);
}
I want to develop app that doesn't have icon launcher. The app will run alarmscheduler which triggered when app is installed or phone is rebooted.
The problem is how can I open the activity since the app doesn't have intent filter like below:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Is there a way to handle dial code such as ##4635*#*# to open the activity ?
or any other solutions are welcomed.
You can do it with two ways:
1) as answer by #KishuDroid
2) By define code in manifest
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
In manifest file
<receiver android:name=".MySecretCodeReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="4635" />
</intent-filter>
</receiver>
Note:
In Second method you must have to dial *#*#your_code#*#* and dont need to press call button
But in first method you can customise your prefix or postfix of code. For example *#your_code# or **your_code##. but you need to press call button.
You have to use Broadcast Receiver...
public class OutgoingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("OutgoingCallReceiver",phonenumber);
Log.i("OutgoingCallReceiver",bundle.toString());
if(code.equals("#056700") {
intent.setComponent(new ComponentName("com.example", "com.example.yourActivity"));
And Your Android Manifest
<receiver android:name="com.varma.samples.detectcalls.receivers.OutgoingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
Also, include the permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
I found lots of threads about this topic, however I'm not able to solve my problem. Here is the code:
The manifest file:
<service
android:name="com.xxx.player.MediaPlayerService.MediaPlayerService"
android:enabled="true" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
</intent-filter>
<receiver android:name="com.xxx.player.MediaPlayerService.MediaPlayerService$ServiceBroadcastReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="#string/ACTION_PREPARE_AND_PLAY_NEW_FILE"/>
<action android:name="#string/ACTION_PREPARE_NEW_FILE"/>
<action android:name="#string/ACTION_START"/>
<action android:name="#string/ACTION_STOP"/>
<action android:name="#string/ACTION_PAUSE"/>
<action android:name="#string/ACTION_SEEK_TO"/>
<action android:name="#string/ACTION_RELEASE"/>
</intent-filter>
</receiver>
</service>
The broadcast class:
public class MediaPlayerService extends Service implements
MediaPlayer.OnErrorListener,
AudioManager.OnAudioFocusChangeListener,
Runnable,
SeekBar.OnSeekBarChangeListener {
...
public class ServiceBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Toast.makeText(getApplicationContext(), "action: " + action, 30000000).show();
if (action.equals(Resources.getSystem().getString(R.string.ACTION_PREPARE_AND_PLAY_NEW_FILE))) {
prepareAndPlayNewFile(intent.getStringExtra("mediaData"));
}
}
}
}
The way I submit the intent:
private void prepareAndPlayNewFile(String mediaData) {
Intent myIntent = new Intent();
myIntent.setAction(context.getString(R.string.ACTION_PREPARE_AND_PLAY_NEW_FILE));
myIntent.putExtra("mediaData", mediaData);
context.sendBroadcast(myIntent);
}
Instead of approaching your playing of media this way, you should instead bind your Service to your Activity instead of using a broadcast receiver for message passing.
Also you shouldn't be using #string/VAR1 (which I'm not sure if intent filters work with that kind of string definition) for your intent actions it ALWAYS should be the constant string such as:
android.intent.action.BLAH
Is there has the way I can get newly installed app from BroadcastReceiver onReceive method.
public class PackageAddedReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Package Installed: ", Toast.LENGTH_LONG).show();
}
}
I have set the filter as:
<receiver android:name=".receiver.PackageAddedReceiver" android:label="Package added Receiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
So what's the best way to get newly installed Package's info?
Thanks.