Chaouki Anass
Chaouki Anass

Reputation: 997

Android java.lang.SecurityException error when calling interstitial ad from adapter

So basically , we have a card view list that contains Image , name , and two download buttons .What I want is after 2 clicks , the interstitial ad should appear for the user . The code works very well for me ( Im using emulator ) but I realised that it gives error for a lot of my users ( usually with android 6.0 , 7.0 and above )

Here is the crash code :

java.lang.SecurityException in com.getlist.MyAdapter.InterstitialInsideshow

java.lang.SecurityException: 

  at android.os.Parcel.readException (Parcel.java:1620)

  at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:183)

  at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:135)

  at android.content.ContentProviderProxy.insert (ContentProviderProxy.java:476)

  at android.content.ContentResolver.insert (ContentResolver.java:1231)

  at android.app.DownloadManager.enqueue (DownloadManager.java:946)

  at com.getlist.MyAdapter.InterstitialInsideshow (MyAdapter.java)
  or                     .access$000 (MyAdapter.java)
  or                     .access$200 (MyAdapter.java)
  or                     .download (MyAdapter.java)
  or                     .onBindViewHolder (MyAdapter.java)
  or                     .onCreateViewHolder (MyAdapter.java)

  at com.getlist.MyAdapter.InterstitialInsideshow (MyAdapter.java)
  or                     .access$000 (MyAdapter.java)
  or                     .access$200 (MyAdapter.java)
  or                     .download (MyAdapter.java)
  or                     .onBindViewHolder (MyAdapter.java)
  or                     .onCreateViewHolder (MyAdapter.java)

  at com.m3u.getlists.MyAdapter$2.onClick (MyAdapter.java)

  at android.view.View.performClick (View.java:5204)

  at android.view.View$PerformClick.run (View.java:21153)

  at android.os.Handler.handleCallback (Handler.java:739)

  at android.os.Handler.dispatchMessage (Handler.java:95)

  at android.os.Looper.loop (Looper.java:148)

  at android.app.ActivityThread.main (ActivityThread.java:5480)

  at java.lang.reflect.Method.invoke (Method.java)

  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)

  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)

MyAdapter.java

package com.getlist;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.squareup.picasso.Picasso;

import java.util.List;

import static android.content.Context.DOWNLOAD_SERVICE;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<list> mylist;
    private Context context;
    private InterstitialAd interstitialinside;

    public MyAdapter(List<com.getlist.list> list, Context context) {
        mylist = list;
        this.context = context;
    }

    public void InterstitialInsideshow()
    {       AdRequest adRequestI = new AdRequest.Builder().build();
            interstitialinside = new InterstitialAd(context);
            interstitialinside.setAdUnitId("ca-app-pub-xxxx/xxxxx"); // I've hide it
            interstitialinside.loadAd(adRequestI);
            interstitialinside.setAdListener(new AdListener() {
                public void onAdLoaded() {  
                    if (interstitialinside.isLoaded()) {
                        interstitialinside.show();
                    }
                  }  
            });
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.mylist,null);
        return new ViewHolder(v);
    }

    private void download(String link, Context context){
        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(link));

        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
        final String filename= URLUtil.guessFileName(link, null, null);
        String file_extension = filename.replace("bin","zip");
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, file_extension);
        DownloadManager dm = (DownloadManager)context.getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
        Toast.makeText(context, "Downloading File", //To notify the Client that the file is being downloaded
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final list list = mylist.get(position);

        // Set Name
        holder.txtname.setText(list.getList_name());
        // Set Image
        Picasso.with(context).load(list.getList_image()).into(holder.img);
        // Set Links
        if(!(list.getList_link1().startsWith("http"))){
            holder.link1.setVisibility(View.INVISIBLE);
        }else{
            holder.link1.setBackgroundColor(holder.link1.getContext().getResources().getColor(R.color.color_nephritis));
            holder.link1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if ((recycleview.x >= 1)) {
                        InterstitialInsideshow();
                        recycleview.x = 0; // Recycleview.x is just a static integer called from another class
                    }else{
                    recycleview.x+=1;
                    }
                    download(list.getList_link1(),context);
                }
            });
        }

        if(!(list.getList_link2().startsWith("http"))){
            holder.link2.setVisibility(View.INVISIBLE);
        }else {
            holder.link2.setVisibility(View.VISIBLE);
            holder.link2.setBackgroundColor(holder.link2.getContext().getResources().getColor(R.color.color_nephritis));
            holder.link2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if ((recycleview.x >= 1)) {
                        InterstitialInsideshow();
                        recycleview.x = 0;
                    }else{
                        recycleview.x+=1;
                    }
                    download(list.getList_link2(),context);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mylist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView img;
        public TextView txtname;
        public Button link1,link2;

        public ViewHolder(View itemView) {
            super(itemView);

            img=(ImageView)itemView.findViewById(R.id.img);
            txtname=(TextView)itemView.findViewById(R.id.txtname);
            link1=(Button)itemView.findViewById(R.id.btnlink1);
            link2=(Button)itemView.findViewById(R.id.btnlink2);
        }
    }
}

list.java

    public class list extends AppCompatActivity {

    private String list_name;
    private String list_image;
    private String list_link1;
    private String list_link2;

   public list(String name,String image,String link1,String link2){
       list_name=name;
       list_image=image;
       list_link1=link1;
       list_link2=link2;
    }

    public String getList_name() {
       return Data_Decryption(list_name);
    }

    public String getList_image() {
        return Data_Decryption (list_image);
    }

    public String getList_link1() {
        return Data_Decryption (list_link1);
    }

    public String getList_link2() {
        return Data_Decryption( list_link2);
    }
}

Manifest.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.getlist">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.READ_PHONE_STATE" tools:node="remove"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:node="remove"/>

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:xlargeScreens="true" />

    <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="com.getlist.first_java" />
        <activity android:name="com.getlist.splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.getlist.privacy_java" />
        <activity android:name="com.getlist.help_java" />
        <activity android:name="com.getlist.recycleview" />
        <activity android:name="com.getlist.webview_java"/>
    </application>

</manifest>

Upvotes: 1

Views: 299

Answers (2)

Manohar
Manohar

Reputation: 23384

Don't extend list with AppCompatActivity .

also in your adapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private List<list> mylist;
private Context context;
private InterstitialAd interstitialinside;

public MyAdapter(List<com.getlist.list> list, Context context) {
    mylist = list;
    this.context = context;
   }
 }

mylist is of type List<list> but in constructor it is List<com.getlist.list> list

make both same

also you are downloading items in adapter but did not check for WRITE_EXTERNAL_STORAGE permission , ask runtime permission for WRITE_EXTERNAL_STORAGE

Upvotes: 1

Fazal Hussain
Fazal Hussain

Reputation: 1127

The app has to request the appropriate permission. You declare that your app needs a permission by listing the permission in the app manifest and then requesting that the user approve each permission at runtime (on Android 6.0 and higher).

Check the permission using the code below

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR)
    != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted

}

for further details check the android developer documentation regarding runtime permission

https://developer.android.com/training/permissions/requesting.html#java

Upvotes: 1

Related Questions