Why the Broadcast receiver is not working dynamicly? - java

I tried to do a broadcast receiver dynamically for incoming SMS will show a toast but the app doesn't show anything.
I register the broadcast dynamically in the mainActivity like this.
MainActivity.java
package com.example.smsreader;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
BroadcastReader reader = new BroadcastReader();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(reader, filter);
}
}
This is brodcast class, the app will show a toast when the phone will receive a SMS but it doesn't
package com.example.smsreader;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BroadcastReader extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if("android.provider.Telephony.SMS_RECEIVED".equals(intent.getAction())){
Toast.makeText(context, "it is working", Toast.LENGTH_LONG).show();
}
}
}
I don't register anything in the manifest file.
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smsreader">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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>
</application>
</manifest>

Try the following :
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");

You need to use the following method for registering local broadcastReceiver ;)
LocalBroadcastManager.getInstance(this).registerReceiver(reader, filter)

Related

Android 8.1 : broadcast receive does not work when I restart the smartphone

I am designing an android application in java and I would like it to launch when the smartphone starts up I of course registered a BroadcastReceiver for the BOOT event of my phone as explained here but it does not work on Android 10 if not for info it worked on Android 4.1.
Can you help me please ?
Here is my code:
Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.openclassrooms.fr.premierprojet">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".PremiereActivite">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".StartAppOnBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
</manifest>
Classe Main :
package com.openclassrooms.fr.premierprojet;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class PremiereActivite extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Classe Diffuseur :
package com.openclassrooms.fr.premierprojet;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartAppOnBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent i = new Intent(context, PremiereActivite.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
Cordially.

How to Listen for Bluetooth Headset Buttons in an Android App?

What is the proper way to handle Bluetooth media buttons in an Android app? At this point, all I want is an event to fire when I press the pause/play button on my Bluetooth headset. Somehow, I had a working solution yesterday, and today it just doesn't work. Without reinstalling the app, or any update happening on my phone, the onReceive method of my subclass of BroadcastReceiver is never entered. I'm targeting Android 9.0, since this is just for myself.
I used the information in these two questions for the solution:
How to capture key events from bluetooth headset with android
BroadcastReceiver for ACTION_MEDIA_BUTTON not working
This is a bare minimum version of what was working yesterday, which currently does not work:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication">
<uses-permission android:name="android.permission.BLUETOOTH" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MediaButtonIntentReceiver">
<intent-filter android:priority="2139999999">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
AudioManager mAudioManager = null;
ComponentName mReceiverComponent = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mReceiverComponent = new ComponentName(this, MediaButtonIntentReceiver.class);
mediaFilter.setPriority(2139999999);
registerReceiver(mMediaButtonReceiver, mediaFilter);
}
}
MediaButtonIntentReceiver.java
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MediaButtonIntentReceiver extends BroadcastReceiver {
public MediaButtonIntentReceiver() {
super();
Log.i("mylog", "init");
}
#Override
public void onReceive(Context context, Intent intent) {
Log.i("mylog", "receive");
abortBroadcast();
}
}

Turning off cell radio when no calls are received

I am trying to detect if any calls are presently being attended or the cell phone is ringing.If not, the cell radio should switch off.Since I am using telephony manager for the first time. I am not able to rectify the error.
The error is " cannot resolve 'setRadioPower(?)'.
My MainActivity.java is:
public class MainActivity extends AppCompatActivity {
private TelephonyManager tm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TelephonyManager telephonyManager =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING){
Toast.makeText(getApplicationContext(),"Phone Is Ringing",
Toast.LENGTH_LONG).show();
}
if(state==TelephonyManager.CALL_STATE_OFFHOOK){
Toast.makeText(getApplicationContext(),"Phone is Currently in A call",
Toast.LENGTH_LONG).show();
}
if(state==TelephonyManager.CALL_STATE_IDLE){
tm.setRadioPower(disabled);
}
}
};
telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
My AndroidManifest.xml is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vk9621.radiocall">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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>
</application>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</manifest>
My imports are:
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
Can someone tell me how to rectify this error?
The reason you won't find it in the developer docs is that it is hidden.
However, you may always find hidden methods in the Android Source Code.
Code reference: public boolean setRadioPower(boolean turnOn){}
If your phone is not rooted, or if you do not have system permission android.Manifest.permission.MODIFY_PHONE_STATE, you won't be able to access this API.
However, if you do have those permissions, you can easily achieve this using the following code:
ITelephony iTelephony = ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
try {
iTelephony.setRadioPower(true);
} catch (RemoteException e) {
e.printStackTrace();
}

onServiceConnected not called after enable my AccessibilityService

After I open my app, it jumps to the accessibility settings. But the onServiceConnected is not called after I have turned on my AccessibilityService (which is called VoiceService).
Could you please tell me what should I do? It seems that the VoiceService fails to start although I enable it in XML. Or I need to bind the MainActivity with the VoiceService in a different way?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wang.hearexpr">
<uses-permission
android:enabled="true"
android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
<application
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=".VoiceService"
android:enabled="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="#string/service_name">
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/voice_config"/>
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"/>
</intent-filter>
</service>
</application>
</manifest>
voice_config.xml
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:description="#string/service_description"
android:packageNames="com.example.wang.hearexpr"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"/>
VoiceService.java
package com.example.wang.hearexpr;
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
public class VoiceService extends AccessibilityService {
private static final String TAG = "hear the voice";
#Override
public void onServiceConnected(){
super.onServiceConnected();
Log.d(TAG, "Service Connected");
}
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d(TAG,"onAccessibilityEvent: "+ event.toString());
}
#Override
public void onInterrupt() {
Log.d(TAG, "Interrupted");
}
#Override
public void onDestroy(){
Log.d(TAG, "Destroyed");
}
}
MainActivity.java
package com.example.wang.hearexpr;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private static final Intent sSettingsIntent =
new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivityForResult(sSettingsIntent,0);
}
}
This is the answer:
in the voice_config.xml
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity" should be modified as your own activity name.

Android Block Incoming SMS using BroadCastReceiver

I want to block incoming SMS message as long as my application is running . I could achieve that but the problem is after I close the app or even restart or even uninstall it the user will not be able to receive SMS message anymore . So How can I make the application to block incoming SMS only when it's running and when it gets closed or uninstalled etc.. to stop blocking SMS. Here's my code :
BroadCastReceiver.Java
package com.example.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class BroadCastReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
abortBroadcast();
}
}
MainActivity.java
package com.example.sms;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Manifiest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".BroadCastReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name="com.example.sms.MainActivity"
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>
</manifest>
Note: I've tested the application on android 2.2 on my android emulator "Sent SMS messages using Telnet"
Check if your app is running then only abort broadcast receiver else not
//define this variable above onReceive() with default value as false;
boolean appRunningInBack=false;
ActivityManager am = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
if(packageName.equalIgnoreCase("your app package name") || appRunningInBack)
{
appRunningInBack=true;
abortBroadCast();
} else {
}
Also add Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.GET_TASKS" />
After this when your app goes in background.or while running .this variable will be true.

Categories

Resources