作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基本上,我们有一个包含图像、名称和两个下载按钮的卡片 View 列表。我想要的是在点击 2 次后,插页式广告应该出现在用户面前。该代码对我来说效果很好(我正在使用模拟器),但我意识到它会给我的很多用户带来错误(通常使用 android 6.0、7.0 及更高版本)
这是崩溃代码:
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>
最佳答案
应用程序必须请求适当的权限。您通过在应用 list 中列出权限然后请求用户在运行时批准每个权限(在 Android 6.0 及更高版本上)来声明您的应用需要权限。
使用下面的代码检查权限
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
有关更多详细信息,请查看有关运行时权限的 android 开发人员文档
https://developer.android.com/training/permissions/requesting.html#java
关于java - 从适配器调用插页式广告时出现 Android java.lang.SecurityException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49775185/
我是一名优秀的程序员,十分优秀!