WifiWizard2 scan returns empty array - android

I'm trying to scan for wifi networks
WifiWizard2.scan().then(function (results) {
console.log("SCANRESULTS", results);
}, function () {
console.log("TOUGH LUCK");
});
But every time I get an empty array
SCANRESULTS []
How am I doing this wrong?
I can confirm there are at least 20 networks around me.
I'm using an android 7 device if it makes any difference.

Sender class
public class WifiFunction {
private final String tag = WifiFunction.class.getSimpleName();
private WifiManager wifiManager;
public List<WifiDetail> getListofWifi(Context context) {
List<WifiDetail> wifiDetails = new ArrayList<>();
List<ScanResult> results = wifiManager.getScanResults();
Log.d(tag,"Wifi Details " + wifiManager.getScanResults().size());
for (ScanResult result : results) {
wifiDetails.add(new WifiDetail(result.BSSID, result.SSID));
Log.d(tag, result.BSSID + result.SSID);
}
return wifiDetails;
}
public void startScan(Context context)
{
wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();
IntentFilter filter = new IntentFilter();
filter.addAction(SCAN_RESULTS_AVAILABLE_ACTION);
context.registerReceiver(new resultReciever(this),filter);
}
}
Receiver class
public class resultReciever extends BroadcastReceiver {
private WifiFunction wifiFunction;
resultReciever(WifiFunction wifiFunction)
{
this.wifiFunction = wifiFunction;
}
#Override
public void onReceive(Context context, Intent intent) {
Log.d("Receiver","started");
wifiFunction.getListofWifi(context);
}
}
From Main Activity I am just calling:
(new WifiFunction()).startScan(this);
Manifest
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permisiion.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permisiion.ACCESS_FINE_LOCATION" />
<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>
<receiver android:name=".resultReciever"/>
</application>
Runtime Permission :
private boolean checkPermission() {
List<String> permissionsList = new ArrayList<String>();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(Manifest.permission.ACCESS_WIFI_STATE);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CHANGE_WIFI_STATE) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(Manifest.permission.CHANGE_WIFI_STATE);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
permissionsList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
}
if (permissionsList.size() > 0) {
ActivityCompat.requestPermissions(this, permissionsList.toArray(new String[permissionsList.size()]),
1);
return false;
}
return true;
}
#Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
switch (requestCode) {
case 1:
(new WifiFunction()).startScan(this);
break;
}
}
This should work. Tried it with the full code.

Related

Android Bluetooth startDiscovery returns false

I want to write a program that will list the available Bluetooth devices and allow the user to pair with them.
I have pieced together the code below. Unfortunately, the only intent that is called is ACTION_STATE_CHANGED, which occurs when I manually enable/disable Bluetooth on the device I am using for testing.
When I manually enable and disable Bluetooth on the device I am using for testing, it does trigger the intent, because I get the corresponding output. However, none of the other intents, such as "Discovery Started" are triggered.
When I run the adapter.startDiscovery() it always returns false, so I do not think it is looking for devices.
This code always returns the device address as 02:00:00:00:00:00
How can I fix this?
public class MainActivity extends Activity {
private BluetoothAdapter BTAdapter;
private ListView mLvDevices;
public static int REQUEST_BLUETOOTH = 1;
private ArrayList<String> mDeviceList = new ArrayList<String>();
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_bluetooth);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
if (!adapter.isEnabled()) {
Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBluetoothIntent);
}
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION},1);
System.out.println("Discovery"+adapter.startDiscovery());
String mydeviceaddress = adapter.getAddress();
String mydevicename = adapter.getName();
System.out.println(mydevicename + " : " + mydeviceaddress+","+adapter.getState());
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("Action"+action);
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
System.out.println("Started");
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
System.out.println("changed");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
System.out.println("finished");
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//bluetooth device found
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// System.out.println("Found device " + device.getName());
}
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.easyinfogeek.bluetooth">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo.Light" >
<activity
android:name="com.easyinfogeek.bluetooth.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>
Check if your app have the follow permissions before start discovery:
Manifest.permission.ACCESS_FINE_LOCATION;
Manifest.permission.ACCESS_COARSE_LOCATION;
Example:
if(ActivityCompat.checkSelfPermission(context,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//without permission, attempt to request it.
requestPermissions(Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION, PERM_REQUEST_CODE);
}
Request Permission:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
if(requestCode == PERM_REQUEST_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//example permission granted
//request startDiscovery again.
}
}
Remember, if you want to enable/disable bluetooth a better way is
BluetoothAdapter.ACTION_REQUEST_ENABLE and
BluetoothAdapter.ACTION_REQUEST_DISABLE.
BluetoothAdapter.ACTION_REQUEST_DISABLE is hide, but you can do it yet.
public static final String ACTION_REQUEST_DISABLE = "android.bluetooth.adapter.action.REQUEST_DISABLE";
//to enable bluetooth
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, REQUEST_ENABLE_BT);
// to disable bluetooth
Intent intent = new Intent(ACTION_REQUEST_DISABLE);
startActivityForResult(intent, REQUEST_DISABLE_BT);

API 28: BroadcastReceiver onReceive never executing

My onReceive is never executed. I've tried following guides online including from the official site. I'm testing on a virtual API 28 device. I understand that some of the GUI-related code might be wrong, but I should at least get a log output right? What am I missing?
Manifest:
<?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.receivesms">
<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"
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=".SMSReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
Receiver, the first Log.D doesn't even execute:
public class SMSReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("MESS", intent.getAction());
Bundle bundle = intent.getExtras();
if (bundle != null) {
SmsMessage[] msgs;
String message;
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length;i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i], bundle.getString("format"));
message = msgs[i].getMessageBody();
Log.d("MESS", message);
((EditText) LayoutInflater.from(context).inflate(R.layout.activity_main, null)
.findViewById(R.id.editText)).append(msgs[i].getDisplayOriginatingAddress()
+ "\n" + message + "\n");
}
abortBroadcast();
}
}
}
Main:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestSmsPermission();
}
private void requestSmsPermission() {
String permission = Manifest.permission.READ_SMS;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[1];
permission_list[0] = permission;
ActivityCompat.requestPermissions(this, permission_list, 1);
} else {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
Log.d("", "Permission granted");
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions,
#NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
requestSmsPermission();
} else {
Toast.makeText(this, "permission not granted", Toast.LENGTH_SHORT).show();
}
}
}
}
Your actual problem is in the SMS PERMISSION. You only request READ_SMS Permission from user not RECEIVE_SMS permission. You also have to give this permission to achieve this.
Manifest.permission.RECEIVE_SMS
Try adding this:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS}, 1);
It looks like:
private void requestSmsPermission() {
String readSms = Manifest.permission.READ_SMS;
String receiveSms = Manifest.permission.RECEIVE_SMS;
int grant = ContextCompat.checkSelfPermission(this, readSms) + ContextCompat.checkSelfPermission(this, receiveSms);
if (grant != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{readSms, receiveSms}, 1);
} else {
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
Log.d("", "Permission granted");
}
}
try to increase the priority so that your app receives the SMS first, I would set it to max:
<intent-filter android:priority="2147483647">

Implement direct calling from Android App

I want to make calls from my app when the user clicks on a button but when the button is click for the first time, the permission dialog box pops up, but after granting the permission the call is not activated with the start activity.Please help me out
public class MainActivity extends AppCompatActivity {
EditText et;
Button call_btn;
Context context;
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
et=(EditText)findViewById(R.id.editText1);
call_btn=(Button)findViewById(R.id.button1);
call_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
try {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:" + et.getText().toString()));
startActivity(i);
}
catch (SecurityException e){
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
String s = writer.toString();
Toast.makeText(getBaseContext(), s, Toast.LENGTH_SHORT).show();
}
}else {
getPermission();
}
}
});
}
public void getPermission(){
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
} else {
}
}
}
Below is what I have in my manifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="flashcodesolution.recharger">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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>
You need to override the onRequestPermissionsResult method and make call again like below:
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:" + et.getText().toString()));
startActivity(i);
}
}
}
Anyway, if you just want to open the built-in Phone app, you can change the action to Intent.ACTION_DIAL which requires no permissions.
Intent i = new Intent(Intent.ACTION_DIAL);
Hope it helps.

Blank screen when open camera in lollipop

am using Zxing barcodescanner and my app is working perfectly , except on Lollipop i have one probleme:
When i open the camera to scan i get blank screen , and a toast to ensure me that permission is granted , but still not working.
I have changed a bit while checking for OS Build Version to Lollipop but i still have the same probleme
Here's my code :
public class QrCodeScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView mScannerView;
private static int camId = Camera.CameraInfo.CAMERA_FACING_BACK;
int number = 0;
String IMEI1="null";
String IMEI2="null";
String id_vendeur;
String id="null";
String username="null";
String points_vendeur="null";
String etat="non";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
id_vendeur = intent.getStringExtra("id_vendeur");
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
Toast.makeText(getApplicationContext(), "Permission already granted", Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
/******************* SP *********************/
final SharedPreferences sharedpreferences = PreferenceManager
.getDefaultSharedPreferences(this);
if (sharedpreferences.getString("etat","").equals("oui")) {
id = sharedpreferences.getString("id", null);
username = sharedpreferences.getString("user", null);
points_vendeur = sharedpreferences.getString("points_vendeur", null);
etat = sharedpreferences.getString("etat", null);
}
}
private boolean checkPermission() {
return ( ContextCompat.checkSelfPermission(getApplicationContext(), CAMERA ) == PackageManager.PERMISSION_GRANTED);
}
private void requestPermission() {
ActivityCompat.requestPermissions(this, new String[]{CAMERA}, REQUEST_CAMERA);
}
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CAMERA:
if (grantResults.length > 0) {
boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;
if (cameraAccepted){
Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(CAMERA)) {
showMessageOKCancel("You need to allow access to both the permissions",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{CAMERA},
REQUEST_CAMERA);
}
}
});
return;
}
}
}
}
break;
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new android.support.v7.app.AlertDialog.Builder(QrCodeScannerActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
#Override
public void onResume() {
super.onResume();
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
if(mScannerView == null) {
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
}
mScannerView.setResultHandler(this);
mScannerView.startCamera(camId);
} else {
requestPermission();
}
}
}
#Override
public void onDestroy() {
super.onDestroy();
mScannerView.stopCamera();
mScannerView = null;
}
#Override
public void handleResult(Result rawResult) {
final String result = rawResult.getText();
/* Log.e("QRCodeScanner", rawResult.getText());
Log.e("QRCodeScanner", rawResult.getBarcodeFormat().toString()); */
final AlertDialog.Builder builder = new AlertDialog.Builder(QrCodeScannerActivity.this)
.setTitle("SCAN IMEI ")
.setCancelable(true)
.setMessage(result)
.setPositiveButton("Valider", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(number+1 == 2) {
IMEI2=result;
number++;
dialog.cancel();
Log.e("Number", " " + number);
Log.e("IMEI : ", result);
Intent intent = new Intent(QrCodeScannerActivity.this, TraitementActivity.class);
intent.putExtra("IMEI1", IMEI1);
intent.putExtra("IMEI2", IMEI2);
intent.putExtra("id_vendeur",id_vendeur);
startActivity(intent);
finish();
}
else{
IMEI1=result;
number++;
dialog.cancel();
Log.e("Number", " " + number);
Log.e("IMEI : ", result);
mScannerView.resumeCameraPreview(QrCodeScannerActivity.this);
}
}
})
.setNeutralButton("Réessayer", new DialogInterface.OnClickListener() { // define the 'Cancel' button
public void onClick(DialogInterface dialog, int which) {
number = 0;
dialog.cancel();
mScannerView.resumeCameraPreview(QrCodeScannerActivity.this);
}
});
builder.show();
}
}
Also , this my gradle configuration :
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.bomarecompany.streamsystem.fidcom"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Finally my manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bomarecompany.streamsystem.fidcom">
<uses-feature android:name="android.hardware.camera" />
<!-- Permissions -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme.Dark">
<activity android:name=".SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:theme="#style/AppTheme.Dark" />
<activity
android:name=".SignupActivity"
android:theme="#style/AppTheme.Dark" />
<activity
android:name=".QrCodeScannerActivity"
android:theme="#style/AppTheme.Dark" />
<activity
android:name=".TraitementActivity"
android:theme="#style/AppTheme.Dark" />
<activity
android:name=".ProduitActivity"
android:theme="#style/AppTheme.Dark" />
<activity
android:name=".NavDrawerActivity"
android:theme="#style/AppTheme" />
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<activity
android:name=".CadeauxActivity"
android:theme="#style/AppTheme.Dark" />
<activity
android:name=".ProfileActivity"
android:theme="#style/AppTheme.Dark" />
<activity
android:name=".ContactActivity"
android:theme="#style/AppTheme.Dark" />
<activity
android:name=".ProposActivity"
android:theme="#style/AppTheme.Dark" />
<activity android:name=".StatistiqueActivity" />
<activity android:name=".NotificationActivity"
android:theme="#style/DakAB"
android:label="Historique Notifications"></activity>
</application>
</manifest>
Thank for your answers !
Check you onResume Function
There is an if condition that camera only initialises for android M and above. need to add else condition and initialise camera.

BroadcastReceiver.onReceive is not called for PHONE_STATE

Following this tutorial: http://www.vogella.de/articles/AndroidServices/article.html#receiver I created my own project. Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="alex.broadcast.sample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="13" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name="MyPhoneReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</application>
</manifest>
Code:
public class MyPhoneReceiver extends BroadcastReceiver {
final String logTag = "BroadcastReceiverSample";
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.i(logTag, "Call state: " + state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i(logTag, "Phone number: " + phoneNumber);
}
}
}
}
Running this sample on Android simulator, I see that it is successfully installed. However, onReceive function is never called. I make incoming call using:
telnet localhost 5554
gsm call 12345678
Emulator shows incoming call, but onReceive is not called.
Shouldn't it be:
<receiver android:name=".MyPhoneReceiver">
^ note the dot
Also, the location of the permission is wrong, it should be a child of <manifest> not of <Application>.
On Android 6 dont forget to allow the permission :
onCreate(){
if (
ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
&&
ActivityCompat.checkSelfPermission(this, Manifest.permission.PROCESS_OUTGOING_CALLS) != PackageManager.PERMISSION_GRANTED
)
{
requestPermission();
}else {
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
registerReceiver(receiver, filter);
}
}
private void requestPermission() {
final List<String> permissionsList = new ArrayList<String>();
permissionsList.add(Manifest.permission.READ_PHONE_STATE);
permissionsList.add(Manifest.permission.PROCESS_OUTGOING_CALLS);
ActivityCompat.requestPermissions(this,permissionsList.toArray(new String[permissionsList.size()]),
REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:{
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.PHONE_STATE");
filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
registerReceiver(receiver, filter);
}
}
}

Categories

Resources