不幸的是,我的Webview App已停止在Marshmallow

时间:2016-04-26 08:51:37

标签: java android android-studio webview android-6.0-marshmallow

我正在android中开发一个WebView应用程序。它在Android Lollipop或它的较低版本中工作正常。但是当我尝试在Marshmallow中运行相同的应用程序时,它会给出错误"很遗憾停止"。

注意:我已使用我的代码更新了问题。

这是我的清单页面。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xamarin.gcmexample">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="com.xamarin.gcmexample.permission.C2D_MESSAGE" />
    <permission
        android:name="com.xamarin.gcmexample.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Main"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".InstanceIdListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <service
            android:name=".RegistrationIntentService"
            android:exported="false"></service>
        <service
            android:name=".MyGcmListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.xamarin.gcmexample" />
            </intent-filter>
        </receiver>
        <activity
            android:name=".Login"
            android:label="@string/title_activity_login"></activity>
    </application>
</manifest>

这是build.gradle文件

apply plugin: 'com.android.application'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    defaultConfig {
        applicationId "com.xamarin.gcmexample"
        minSdkVersion 11
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.google.android.gms:play-services:8.4.0'
}

这是主要活动

package com.xamarin.gcmexample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import android.telephony.TelephonyManager;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.logging.Logger;
public class Main extends AppCompatActivity {
    WebView mWebView;
    Logger LOGGER;
    Button btnlogout;
    TextView lblname;
    private static Main  mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(R.layout.main);
            mWebView = (WebView) findViewById(R.id.activity_main_webview);
            btnlogout = (Button) findViewById(R.id.btnlogout);
            LOGGER = Logger.getLogger(RegistrationIntentService.class.getName());
            lblname = (TextView) findViewById(R.id.lblname);
            int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(Main.this);
            final Intent intentlogin = new Intent(this, Login.class);          
            if(isNetworkAvailable()) {
                WebSettings webSettings = mWebView.getSettings();
                webSettings.setJavaScriptEnabled(true);
                mWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "Android");
                mWebView.setWebViewClient(new MyWebViewClient(this));
                mContext = Main.this;
                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                String id = telephonyManager.getDeviceId();
                if (resultCode == ConnectionResult.SUCCESS) {
                    lblname.setText("Michael");                               
                    mWebView.loadUrl("https://m.facebook.com?imei="+id);
                    LOGGER.info("After LoadUrl ");
                } else {
                    LOGGER.info("Google Play Services is not Available ");
                    Toast toast = Toast.makeText(getApplicationContext(), "Google Play Services is not Available", Toast.LENGTH_LONG);
                    toast.show();
                }
            }
            else
            {
                Toast toast = Toast.makeText(getApplicationContext(), "Internet is not Available", Toast.LENGTH_LONG);
                toast.show();
                lblname.setText("No Internet");
            }
            btnlogout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LOGGER.info(" startService(intent);");
                    startActivity(intentlogin);
                }
            });
        }catch(Exception ex)
        { LOGGER.info(" mWebView:"+ex.getMessage());
            return;
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        setContentView(R.layout.main);
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        btnlogout = (Button) findViewById(R.id.btnlogout);
        lblname=(TextView)findViewById(R.id.lblname);
        LOGGER = Logger.getLogger(RegistrationIntentService.class.getName());
        final Intent intentlogin = new Intent(this, Login.class);
        int resultCode = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(Main.this);    
        if(isNetworkAvailable()) {
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            mWebView.addJavascriptInterface(new MyJavaScriptInterface(this), "Android");
            mWebView.setWebViewClient(new MyWebViewClient(this));
            mContext = Main.this;
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            String id = telephonyManager.getDeviceId();
            if (resultCode == ConnectionResult.SUCCESS) {
                lblname.setText("Michael");
                mWebView.loadUrl("https://m.facebook.com?imei="+id);
                LOGGER.info("After LoadUrl ");
            } else {
                LOGGER.info("Google Play Services is not Available ");
                Toast toast = Toast.makeText(getApplicationContext(), "Google Play Services is not Available", Toast.LENGTH_LONG);
                toast.show();
            }
        }
        else
        {
            Toast toast = Toast.makeText(getApplicationContext(), "Internet is not Available", Toast.LENGTH_LONG);
            toast.show();
            lblname.setText("No Internet");
        }
        btnlogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LOGGER.info(" startService(intent);");
                startActivity(intentlogin);
            }
        });
    }

    public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();        
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }
}

 class MyWebViewClient extends WebViewClient {
     private Context context;
     public MyWebViewClient(Context con)
     {
         this.context=con;
     }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {       
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);  
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
    }

    @Override
    public void onReceivedError (WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);       
    }
}

class MyJavaScriptInterface {
    Activity activity;
    MyJavaScriptInterface(Activity activity) {
        this.activity = activity;
    }

    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(activity, "On Javascript showtoast() "+toast, Toast.LENGTH_LONG).show();
    }

    @JavascriptInterface
    public void closeActivity() {
        activity.finish();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在onCreate of your activity中执行此操作

 webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new MyWebViewClient());
    webView.loadUrl("your url link");

然后

private class MyWebViewClient extends WebViewClient{

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
         webView.loadUrl("your url link");
        return true;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        ....code here
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
       .... code here
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        ,.....code here
    }
}

希望它可以帮到你。