androidinvoke
『壹』 如何在android APP中設置系統語言
設置達到的效果
在設置界面打開切換語言的界面,選擇語言後重啟 HomeActivity,語言切換完成,下次重新打開 App ,也是用戶設置的語言。
工具
編程軟體;
實現步驟
在不同的 value 文件夾下添加不同語言的string.xml文件,項目添加了英文、簡體中文、繁體中文三種語言,如下圖所示:
『貳』 android怎麼實現自動接聽和掛斷電話功能
android 實現來電自動接聽和自動掛斷的方法:
第一步:准備應用環境需要的系統包和aidl文件。
(1)在應用中創建包:android.telephony
將android系統框架下的\framework\telephony\java\android\telephony目錄中的NeighboringCellInfo.aidl文件復制到上面創建的包(android.telephony )中;
(2)在應用中創建包:com.android.internal.telephony
將android系統框架下的\framework\telephony\java\com\android\internal\telephony目錄中的ITelephony.aidl文件復制到上面創建的包(com.android.internal.telephony )中。
第二步:創建一個獲取ITelephony的方法
PhoneUtils.java
package com.zhouzijing.android.demo;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.telephony.TelephonyManager;
public class PhoneUtils {
/**
* 根據傳入的TelephonyManager來取得系統的ITelephony實例.
* @param telephony
* @return 系統的ITelephony實例
* @throws Exception
*/
public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {
Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);//私有化函數也能使用
return (ITelephony)getITelephonyMethod.invoke(telephony);
}
}
第三步:創建電話廣播攔截器
MyPhoneBroadcastReceiver.java
package com.zhouzijing.android.demo;
import com.android.internal.telephony.ITelephony;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MyPhoneBroadcastReceiver extends BroadcastReceiver {
private final static String TAG = MyPhone.TAG;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "[Broadcast]"+action);
//呼入電話
if(action.equals(MyPhone.B_PHONE_STATE)){
Log.i(TAG, "[Broadcast]PHONE_STATE");
doReceivePhone(context,intent);
}
}
/**
* 處理電話廣播.
* @param context
* @param intent
*/
public void doReceivePhone(Context context, Intent intent) {
String phoneNumber = intent.getStringExtra(
TelephonyManager.EXTRA_INCOMING_NUMBER);
TelephonyManager telephony = (TelephonyManager)context.getSystemService(
Context.TELEPHONY_SERVICE);
int state = telephony.getCallState();
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG, "[Broadcast]等待接電話="+phoneNumber);
try {
ITelephony iTelephony = PhoneUtils.getITelephony(telephony);
iTelephony.answerRingingCall();//自動接通電話
//iTelephony.endCall();//自動掛斷電話
} catch (Exception e) {
Log.e(TAG, "[Broadcast]Exception="+e.getMessage(), e);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i(TAG, "[Broadcast]電話掛斷="+phoneNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(TAG, "[Broadcast]通話中="+phoneNumber);
break;
}
}
}
第四部:注冊電話廣播攔截器
MyPhone.java
package com.zhouzijing.android.demo;
import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
public class MyPhone extends Activity {
public final static String TAG = "MyPhone";
public final static String B_PHONE_STATE = TelephonyManager.ACTION_PHONE_STATE_CHANGED;
private MyPhoneBroadcastReceiver mBroadcastReceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_phone);
}
//按鈕1-注冊廣播
public void registerThis(View v) {
Log.i(TAG, "registerThis");
mBroadcastReceiver = new MyPhoneBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(B_PHONE_STATE);
intentFilter.setPriority(Integer.MAX_VALUE);
registerReceiver(mBroadcastReceiver, intentFilter);
}
//按鈕2-撤銷廣播
public void unregisterThis(View v) {
Log.i(TAG, "unregisterThis");
unregisterReceiver(mBroadcastReceiver);
}
}
第5步:在AndroidManifest.xml配置許可權
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
其中:
iTelephony.answerRingingCall();//自動接通電話
必須有許可權 android.permission.MODIFY_PHONE_STATE
iTelephony.endCall();//自動掛斷電話
必須有許可權 android.permission.CALL_PHONE。
『叄』 android開發中,webview常用於顯示網頁或h5頁面,一個遇到跨域方面的坑。
1.跨域cookie讀取
什麼是跨域,簡單的說就是不同的域名,我們都知道在pc上我們用瀏覽器訪問網址,不同的網址都會在本地存儲一些cookie信息,這樣就可以實現比如自動登錄等功能,在pc上不同域名是不能相互讀取其他域下的cookie信息的(非web專業開發人員,如果理解有誤,歡迎指出)。
但是在 android 上在api 23之前,是可以跨域讀取cookie的,比如A域寫入一個userId的cookie,B域可以讀取該值。但是在23時,系統將該值設置成了false,不再讓跨域讀取了。如果你的應用有跨域讀取需求,怎麼辦?可以採用如下方式進行開啟:
/*** 設置跨域cookie讀取*/
public final void setAcceptThirdPartyCookies() {
//target 23 default false, so manual set true
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { CookieManager.getInstance().setAcceptThirdPartyCookies(webView,true);
}
}
2.http/https混合載入
在現階段,很多網站都改成了https進行訪問,https可以提升訪問網站的安全性,防止信息被竊取,如果所有的網頁都是https且網頁內的鏈接也是都是https,那就沒有混合載入(文本區域https,圖片文件http載入)的問題了。但是很多資源現階段還沒有改變成https訪問,往往頁面都嵌入了http的鏈接。這種混合網頁如果不進行處理,直接載入是會出現錯誤的。怎麼解決這個問題?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
這也是一個分版本的函數,在api23之前,默認是可以混合載入的,但是在23時,默認值改成了MIXED_CONTENT_NEVER_ALLOW,因此如果你有混合載入的需求,設置setMixedContentMode為MIXED_CONTENT_ALWAYS_ALLOW。
3.無法解決跨域訪問問題,可以嘗試給webview設置如下配置,已解決該問題;
if (Build.VERSION.SDK_INT >= 16) {
Class clazz =webView.getSettings().getClass();
Method method = clazz.getMethod("", boolean.class);
if (method != null) {
method.invoke(webView.getSettings(), true);
}
}
} catch (IllegalArgumentExceptione) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}