My Broadcast Receiver does not start - android

I have some problem with BroadcastReceiver. There nothing happens when I catch an outgoing call.
public class demoBroadcastReceiver extends BroadcastReceiver {
/** Called when the activity is first created. */
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "there is new calling", Toast.LENGTH_LONG).show();
}
}
This is content of my Manifest:
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".demoBroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
Updated:
Thanks Lucifer for solution:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
I also need this:
<uses-permission android:name="android.permission.NEW_OUTGOING_CALL" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />**strong text**

You should declare this permission in your AndroidManifest.xml file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

maybe you should post your log here, so we can analyse it.
try add this permission:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Hope this

put this in your manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
and this in your application node..
<receiver android:name="com.example.demoBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" /> <--- add this intent filter
</intent-filter>
</receiver>
this is to start a service when a device boots up on android

Related

BroadcastReceiver doesn't receive the broadcast event message

I created my broadcast receiver AnprEventReciever that should be triggered when connectivity state changes, however it doesn't.
AnprEventReciever:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import bi.anpr.layouts.SplashActivity;
public class AnprEventReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("test","event recieved");
Toast.makeText(context, "event recieved" , Toast.LENGTH_LONG).show();
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sxx.vlctest">
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="bi.anpr.vlc.VLCApplication"
android:allowBackup="false"
android:icon="#mipmap/ic_anpr_launcher_round"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name="bi.anpr.core.AnprEventReciever"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="bi.anpr.layouts.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.VideoResourceActivity"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.ZoneActivity"
android:label="Zone Setter"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.SettingsActivity"
android:label="#string/title_activity_settings"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.SplashActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
</manifest>
Please note that i'd tested almost every possible action that all failed except the BOOT_COMPLETED, where my BroadcastReceiver was successfully fired.
Thanks for #TheWanderer who helped me through his comment to find the answer. In the Android 8.0 Behavior Changes Note developers mentioned the following:
Apps cannot use their manifests to register for most implicit
broadcasts (that is, broadcasts that are not targeted specifically at
the app).
So i tried to programatically registering my BroadcastReceiver like this below instead of creating a manifest entry for it:
private AnprEventReciever myReceiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_resource);
filter = new IntentFilter();
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
myReceiver = new AnprEventReciever();
}
#Override
protected void onResume() {
super.onResume();
registerReceiver(myReceiver, filter);
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(myReceiver);
}
It works perfectly, however unfortunately the action "android.intent.action.CONNECTIVITY_CHANGE" seems to be deprecated where i couldn't find it in the Intent class and i don't know if it is found elsewhere.
Note: as i go through reading i found that it is necessary to un-register the broadcast receiver when the activity pauses or destroys, otherwise you might get an error when trying to register or re-register it.
UPDATE:
Create intent filter with ConnectivityManager.CONNECTIVITY_ACTION instead of "android.intent.action.CONNECTIVITY_CHANGE" in order to receive connectivity changed broadcasts.

receive sms broadcast after clear ram

i know this question may be asked by another user ...but i can not solve my problem
i create one broadcast for receive sms and it worked well but when user clear ram broadcast does not work ...
how i can create a broadcast that work even user clear ram
it is my code
public class ReceiveSms extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("log","sms received");
// Toast.makeText(App.context,"you have sms",Toast.LENGTH_SHORT).show();
Object[] pdus= (Object[]) intent.getExtras().get("pdus");
SmsMessage sms=SmsMessage.createFromPdu((byte[]) pdus[0]);
String body=sms.getMessageBody();
String sender=sms.getDisplayOriginatingAddress();
Log.i("log","sms body"+body);
Toast.makeText(App.context,"message from :"+sender,Toast.LENGTH_SHORT).show();
Intent startProgram=new Intent(App.context,MainActivity.class);
startProgram.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
App.context.startActivity(startProgram);
}
}
and mainfast
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.masiha68.sms">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".RC"
android:process=":remote"
android:enabled="true"
>
</service>
<receiver android:name=".ReceiveSms"
android:process=":remote"
>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED">
</action>
</intent-filter>
</receiver>
</application>
</manifest>
In huawei device, you need to enable you app in 'Protected apps' to keep running after the screen off or after you kill app from background.
For that goto,
Phone Manager -> Power saving -> Protected apps -> find your app and 'enable' it.

Code style / correct structure android

Hello fellow programmers,
I cant find a proper way to route my application, I would like one file to decide which activity has to be started
I am building an android application that in general contains:
-2 ways to start up
-3 activity's
The application can be launched by clicking the icon (standard launch)
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
And an activity is started when there is an incoming call
<action android:name="android.intent.action.PHONE_STATE" />
In the first setup there was 2 activity's one to register and one to use the application. In this
setup I added an extra activity to check if a token was present and route to the right activity.
In the setup that I have now, all the actions are in the of a broadcast receiver. I tried implementing the logic of the extra activity.
The problem I am facing is the check of the incoming call, it is always launching the same activity CallHandler,
I think routing the application within a broadcast receiver might be bad practice, but I can not find a better way of routing the application, and the current code does not work.
Your help is very much appreciated, the following pieces of code might help explain my issue:
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".RoutingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<activity
android:name=".RegisterActivity"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
<activity
android:name=".CallHandler"
android:label="#string/title_activity_main" >
</activity>
<activity
android:name=".CreateCallActivity"
android:label="#string/title_activity_create_call" >
</activity>
</application>
RoutingCallReceiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class RoutingCallReceiver extends BroadcastReceiver {
TelephonyManager telephony;
Intent in;
public void onReceive(Context context, Intent intent) {
PrimePhoneStateListener phoneListener = new PrimePhoneStateListener();
telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
if(telephony.getCallState()== 1){
in = new Intent(context, CallHandler.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
}
else {
TokenIO tokenHandler = new TokenIO();
String token = tokenHandler.getToken(context);
Log.d("AAfter", "Token");
if(token.equals("") || token.equals(null)){
in = new Intent(context, RegisterActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
}
else{
in = new Intent(context, CreateCallActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
}
}
}
// when finish your job, stop listen to changes
public void onDestroy() {
telephony.listen(null, PhoneStateListener.LISTEN_NONE);
}
}
you can make change in your xml file
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".RoutingCallReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity
android:name=".RegisterActivity"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize|stateVisible" >
</activity>
<activity
android:name=".CallHandler"
android:label="#string/title_activity_main" >
</activity>
<activity
android:name=".CreateCallActivity"
android:label="#string/title_activity_create_call" >
</activity>
</application>
and put this tag into main activity
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
and i hope that will work fine.

android send action_call from broadcastreceiver

i want to forward all phone calls after receive a sms , so i have to using broadcastreceiver to listen incoming sms , this is my code :
public class SMSListiner extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED")) {
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + Uri.encode("*21*02164371348#")));
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
}}
and this is manifest
`
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arash.SMSListiner">
<uses-sdk android:minSdkVersion="10"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<action android:name="android.intent.category.LAUNCHER"></action>
</intent-filter>
</activity>
<receiver android:name=".SMSListiner">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.BROADCAST_SMS"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
</manifest>
`
call is sent but i have an error :
"call forwarding , connection problem or invalid MMI Code"
but when i try this code to send action_call in activity i have no problem and it work correctly ,
any idea ?

Boot complete in ICS

I am having a broadcast receiver that starts a service in ICS. I have not activity associated with the code. The issue that I am facing is that I am unable to receive the BOOT COMPLETED broadcast in my receiver. Is there a round about solution for this. To receive a boot complete intent in ICS. I read that
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
but what is the soultion. Can anyone help in this.
enter code here
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="11" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.Translucent.NoTitleBar">
<receiver
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:name=".BootComp"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<service android:name="CheckForUpdateService"></service>
<activity android:name="Upgrade_choice"></activity>
</application>
This is my manifest file
This is my method in the receiver to receive the boot completed. I am starting a service on boot complete of the system.
if(("android.intent.action.BOOT_COMPLETED").equals(intent.getAction()))
{
Log.d(tag,"Received the BOOT COMPLETE intent");
Intent i=new Intent();
i.setAction("com.example.bootcompletecheck.CheckForUpdateService");
ctx.startService(i);
}
else
{
Log.d(tag,"----Not received----");
}
}
}

Categories

Resources