Problem when calling with a button in an android widget

0

I have the following problem: I have created a widget that contains a text and a button that says call. I have added the permissions and the corresponding manifest, but even so I can not reach that by pressing the button, this one calls.

this is the Widget.java:

    package io.movilidadcdelu.starter;
import android.Manifest;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.widget.Button;

public class widget extends AppWidgetProvider {

  static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                              int appWidgetId) {
  }

    public class Widget extends Activity {

      private Button button;

      public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.widget);

        button = findViewById(R.id.button_call);

        // add button listener
        button.setOnClickListener(arg0 -> {

          Intent callIntent = new Intent(Intent.ACTION_CALL);
          callIntent.setData(Uri.parse("tel:101"));
          if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
          }
          startActivity(callIntent);

        });

      }
    }
  }

and widget.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="@dimen/widget_margin">

  <TextView
    android:id="@+id/policia"
    android:layout_width="106dp"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="25dp"
    android:layout_marginTop="28dp"
    android:fontFamily="sans-serif"
    android:text="Policia"
    android:textAlignment="center"
    android:textSize="24sp"
    android:textStyle="bold"
    android:typeface="sans" />

  <Button
    android:id="@+id/button_call"
    android:layout_width="109dp"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="25dp"
    android:layout_marginTop="68dp"
    android:text="Llamar" />
</RelativeLayout>

and finally the manifest

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="2" android:versionName="1.1.0" package="io.movilidadcdelu.starter" xmlns:android="http://schemas.android.com/apk/res/android">
    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">

      <receiver android:name="io.movilidadcdelu.starter.widget" >
        <intent-filter>
          <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
          android:resource="@xml/widget_info" />
      </receiver>

      <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="io.movilidadcdelu.starter.MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
            <intent-filter android:label="@string/launcher_name">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.sarriaroman.PhotoViewer.PhotoActivity" android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" />
    </application>
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="26" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.telephony" android:required="false" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
</manifest>
    
asked by Nahuel Akeen Reymundo 07.07.2018 в 20:43
source

2 answers

0

Although you ask for permission when you press the button, you have to declare it in your manifest too

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    
answered by 08.07.2018 в 15:16
0

Look to see if this example helps you.

//Comprobar version actual de android q estamos corriendo
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        //para versiones nuevas de android
                        //Comproboar si nunca ha aceptado, si ha aceptado o si ha denegado
                        if (CheckPermission(Manifest.permission.CALL_PHONE)) {
                            //Ha aceptado
                            Intent intentCall = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
                            if (ActivityCompat.checkSelfPermission(NoteActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
                                return;
                            startActivity(intentCall);
                        } else {
                            //Ha denegado o es la primera vez
                            if (!shouldShowRequestPermissionRationale(Manifest.permission.CALL_PHONE)) {
                                //No se ha preguntado aun
                                requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, PHONE_CALL_CODE);
                            } else {
                                //Ha denegado
                                Toast.makeText(NoteActivity.this, "Please, enable the permission", Toast.LENGTH_SHORT).show();
                                Intent intentSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                intentSettings.addCategory(Intent.CATEGORY_DEFAULT);
                                intentSettings.setData(Uri.parse("package:" + getPackageName()));
                                intentSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intentSettings.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                                intentSettings.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                                startActivity(intentSettings);
                            }
                        }
                    }

I hope you give a guide at least if you solved your problem I advise you to publish it but for what I see no. So here you are.

    
answered by 10.07.2018 в 17:26