BroadcastReceiver dont catch the phone state - android

I am trying to start a new app, but i need to know about a change in the phone state...
for some reason (i am new to this) i cant catch the broadcast of the change.
thats my code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("TAG","yyyyyyyyyyyy");
}
}
and thats my manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
</application>
as you can see... very simple.
but i cant see the Log in the onReceive on my LogCat.
any one knows why?
Thanks!

Due to this and this, your BroadcastReceiver won't receive anything. The solution is to design a simple activity for your app that starts at least once after installation, before it starts receiving.

In order for a Manifest based Broadcast Receiver to get registered, you need to run your app by opening it from the app drawer. If you don't do that, then the receiver will not be called by Android. So basically, you need at least one Activity that can start - it does not have to do anything, it just has to be started at least once. Did you do that?
This is true after Android 3.0+

Related

Why BroadcastReceiver don't work in React Native?

I try to develop an application that in the future can run a service, the service I want to run when I connect to a network.
At the moment I only need the receiver to print in the log (using react-native log-android) that has been connected or disconnected, or to visualise a toast with a message, all this in the background not in the foreground.
Try the following,
First I put the receiver in the AndroidManifest.xml
<application>
...
<receiver android:name="com.air_fighers_react_native.receiver.NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
Even add the permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Create the receiver folder in java > com > air_fighters_react_native > receiver and inside the NetworkChangeReceiver.java file with the following code:
package com.air_fighters_react_native.receiver;
import android.content.BroadcastReceiver
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class NetworkChangeReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Cambio de conexion.", Toast.LENGTH_LONG).show();
}
}
With this it is supposed that it should be enough, however when running the application there are no problems, but when I close the application and activate and deactivate the WiFi the toast message is not displayed, not even when I have the application in the foreground.
I have already tried changing receiver to:
<receiver android:name=".NetworkChangeReceiver">
And changing NetworChangeReceiver to the same level as MainApplication and MainActivity in folders.

Application have Only BroadcastReceiver is not working

I have only one CallBroadcastReceiver which extends BroadcastReceiver and a menifest where its declared.
Still its not showing the Toast while I am placing a outgoing call.
Can you please help?
its showing
[2016-02-27 10:02:20 - OnlyReciever] No Launcher activity found!
[2016-02-27 10:02:20 - OnlyReciever] The launch will only sync the application package on the device!
Code Below -
CallBroadcastReceiver.class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* #author Cosmos
*
*/
public class CallBroadcastReceiver extends BroadcastReceiver
{
public CallBroadcastReceiver() {}
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ind.example.onlyreciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-sdk
android:minSdkVersion="22"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name="ind.example.onlyreciever.CallBroadcastReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>
For Android 3.1 and higher,
You have to launch one of your activities to get your app out of stopped state before any manifest-registered BroadcastReceiver will work, as detailed in the 3.1 release notes.
When the app is first installed or manually force-closed, it is in "stopped state". In this state, only broadcast intents with the FLAG_INCLUDE_STOPPED_PACKAGES flag will reach your broadcast receivers; this flag is not included in the default system broadcasts, so the app cannot receive them in the stopped state.
Note that "stopped state" is tracked by the package manager and is not the same as "application not running". Once the app is out of stopped state, it will remain so even if you reboot your device and your app is not running.

Android App with Broadcast receiver after reboot as main

Hello i would to develop a simple app without Main Activity as launcher.
I want to register a broadcast receiver which starts after reboot of device and inside OnReceive callback starts an Activity
Here my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.examples"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<receiver android:name=".AfterRebootBR" android:exported="false"
android:label="Boot Notification Receiver" android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
And here my Broadcast receiver
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.examples;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class AfterRebootBR extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("AfterRebootBR","***************** ON RECEIVE *********************");
Log.e("AfterRebootBR","***************** ON RECEIVE *********************");
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
And finally the MainActivity
package it.examples;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
What is wrong in my code?
Thanks in advance
Francesco
my code is working..here is it...
in manifest
<receiver
android:name="com.calender.calenderevent.Reboot_Reciever"
android:enabled="true"
android:exported="true"
android:label="BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
I cant see anything wrong with your code, however i have something worth to try.
Move the permission out of the application tag :
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
If its not working, simplify the receiver :
<receiver android:name=".AfterRebootBR">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Still not working? Try to add some delay, as mentioned here :
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
//run your service
}
}, 10000);
Quoted from the link above :
While, I suggestion to delay several seconds, e.g., 10 seconds, before
running the (1) line, which is more stable for different phones and
services.
For example, in my case, my service is going to write sd card. If you
start your service immediately, some phones may fail because the sd
card is not ready.
Starting with android 3.1 you cannot have a broadcast receiver to get spawned by the application service manager if it has no context in active state (aka. at least an activity or service that is keeping the process in an "active state")
Excerpt from specification
Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents.
It does this to prevent broadcasts from background services from inadvertently or
unnecessarily launching components of stoppped applications. A background service or
application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES
flag to broadcast intents that should be allowed to activate stopped applications.
Applications are in a stopped state when they are first installed but are not yet
launched and when they are manually stopped by the user (in Manage Applications).
You need to somehow start your application, and then send it in a dormant state (but registered in the app manager). You can use a service for this.
It is strongly NOT recommended to start Activity from BroadcastReciever:
https://developer.android.com/training/run-background-service/report-status.html#ReceiveStatus
Never start an Activity in response to an incoming broadcast Intent.
In my case PackageManager.DONT_KILL_APP helped:
https://developer.android.com/training/scheduling/alarms.html#boot
ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
I've experimented with MIUI 8 firmware real device Xiaomi Redmi Note 3.
My findings are:
you have to add app to autorun to enable it be fired by broadcast. I've checked it with such serious apps as Viber, WhatsApp.
I've compared with manifest settings (without enabling reciever programmatically):
<receiver
android:name=".activities.broadcastrecievers.CallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>

Start FtpServer when charging the Phone

i want to make an App which starts an FTPServer when my Phone is conncted to Power. I dont want my App to have an GUI.
In GooglePlay i found an 3rd Party FtpServer App which can be started and stopped via Intent
Intents:
com.theolivetree.ftpserver.StartFtpServer
com.theolivetree.ftpserver.StopFtpServer
So i would like to use those codes in my app to execute those commands but i dont have any clue how to use them. So far i am trying to use Toast to look if anything happen before i move further, but unfortunally nothig happens.
my AndroidManifest.XML looks like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.power.ftpserver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:allowBackup="true">
<receiver android:enabled="true" android:name="FtpReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"> </action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
FtpReceiver.java
package de.power.ftpserver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class FtpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context , Intent intent)
{
String action = intent.getAction();
if(action.equals(Intent.ACTION_POWER_CONNECTED))
{
Toast.makeText(context, "FtpServer gestartet", Toast.LENGTH_LONG).show();
}
else if(action.equals(Intent.ACTION_POWER_DISCONNECTED))
{
Toast.makeText(context, "FtpServer beendet", Toast.LENGTH_LONG).show();
}
}
}
When i install the App to my Phone the Console says
[2013-08-02 12:28:19 - FtpServer] No Launcher activity found!
[2013-08-02 12:28:19 - FtpServer] The launch will only sync the application package on the device!
but installs anyway. I can Plug in and Out the Powerconnectioncable but i dont get any Toast Messages. Can anyone help plz?
I know this is a bit old. But just for future reference and as a partial solution, I had the same problem of how to use the Intents provided by the FTP Server app, this is what you're looking for:
Intent startIntent = new Intent("com.theolivetree.ftpserver.StartFtpServer");
sendBroadcast(startIntent); // start
And to stop, it's the same
Intent startIntent = new Intent("com.theolivetree.ftpserver.StopFtpServer");
sendBroadcast(startIntent); // stop
Hope it still can help someone.

Unable to receive android.intent.action.EVENT_REMINDER broadcast

I would like to write an application that is triggered when a calendar reminder occurs. I realize there is no officially documented way of doing this, but I have seen in the log that when my calendar alarm goes off on my phone (Droid X), AlertReceiver indicates that it has received an android.intent.action.EVENT_REMINDER:
01-03 11:03:00.029 D 1523 AlertReceiver onReceive: a=android.intent.action.EVENT_REMINDER Intent { act=android.intent.action.EVENT_REMINDER dat=content://com.android.calendar/129407058000 flg=0x4 cmp=com.android.calendar/.AlertReceiver (has extras) }
So, I set up a simple BroadcastReceiver:
package com.eshayne.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class CalendarTest extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
android.util.Log.i("CalendarTest", "CalendarTest.onReceive called!");
}
}
with this manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eshayne.android"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.READ_CALENDAR" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true">
<receiver android:name="com.eshayne.android.CalendarTest">
<intent-filter>
<action android:name="android.intent.action.EVENT_REMINDER" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
Unfortunately, when I put this on my phone and set up a calendar event with a reminder - when the reminder alerts, I still see the AlertReceiver log entry, but not mine.
I have also read here about some system intents that require registering via code rather than in the manifest. So, I tried the following instead:
package com.eshayne.android;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
public class CalendarTestDisplay extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
android.util.Log.i("CalendarTestDisplay", "received broadcast");
}
},
new IntentFilter("android.intent.action.EVENT_REMINDER"));
}
}
with this modified manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eshayne.android"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.READ_CALENDAR" />
<application android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true">
<activity android:name=".CalendarTestDisplay"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
with no better result.
Any ideas what I may be missing? Or any other ideas of how I might be able to capture calendar alarm occurrences?
Thanks,
Ethan
You need to set data scheme to "content" in the intent filter.
Using manifest, add data element inside intent-filter
<receiver android:name="com.eshayne.android.CalendarTest">
<intent-filter>
<data android:scheme="content"/> <!-- this was missing -->
<action android:name="android.intent.action.EVENT_REMINDER" />
</intent-filter>
</receiver>
Using code, add datascheme in one function call
IntentFilter filter = new IntentFilter(CalendarContract.ACTION_EVENT_REMINDER);
filter.addDataScheme("content"); // this was missing
registerReceiver(myRemindersReceiver, filter);
Well, what you're trying to do is not part of the Android SDK, mostly because the calendar is not part of the operating system.
That being said, at minimum, you will need to add a <data> element to your <intent-filter>, since the Intent has a Uri.
However, I'm reasonably certain that this will not work, since the Intent also specifically identifies a component (com.android.calendar/.AlertReceiver). AFAIK, that was in the Intent at the outset, and therefore the Intent will only be delivered to that component, ignoring all other routing rules. It's conceivable the listed component only showed up after Intent resolution, but I don't think that's how those log entries work.
It is possible to make the broadcast intent "android.intent.action.EVENT_REMINDER" work by specifying DataAuthority and DataScheme in your intent filter along with your intent action.You need to specify DataAuthority as "com.android.calendar" and DataScheme as "content".
the broadcast intent "android.intent.action.EVENT_REMINDER" only gets fired when an alarm notification needs to be posted for a reminder
set a notification for your EVENT (Eg: 10 minutes before the event) in order for the broadcast intent "android.intent.action.EVENT_REMINDER" to work

Categories

Resources