我只是按照你所說的去做,創建對話框併發送給設置;我假設你已經設法拜訪了中肯的時間onError的FCN,所以後來
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(getString(R.string.loc_man));
builder.setMessage(getString(R.string.ask_for_gps));
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getBaseContext(), getString(R.string.gps_no), Toast.LENGTH_SHORT).show();
finish();
}
});
builder.create().show();
你也需要做的是從你的JavaScript調用這個函數......哦,這很有趣,B/c你會一次性使用很多非常酷的java概念,我只和Cordova一起工作,但這應該都是一樣的=]請記住,如果你在我的代碼中看到一些未定義的變量,你應該可能認爲它是一個領域。
因此,假設您確定此代碼遇到錯誤,我們來編寫一個接口名稱;對「Android」是一個非常合乎邏輯的一個
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
Android.TurnOnTuneIn();
}
現在又回到你的Java,在你的PhoneGap的應用grep來找到哪裏有「的WebView」;如果它與我的cordova實現類似,它會在某處放置webview.loadUrl()。打開定義這個類的文件;這是編輯/放置我們正在處理的所有java的問題。在'公共類PhoneGapActivity擴展Activity'之後,插入'implements CordovaInterface'(我認爲不是PhoneGapInterface);如果你的IDE給你提供了一個實現抽象方法的選項的錯誤,那麼重磅。
確認的WebView是在類費爾德,而不是在一個方法的變量,然後
//CordovaWebView should work, but maybe it needs to be PhoneGapWebView, you're smart, you'll figure it out =]
webView = (CordovaWebView) findViewById(R.id.data_window);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); //this always seemed like a good idea to me
webView.addJavascriptInterface(new JSInterface(this), "Android"); //see the interface name? =]
//this line should already be in this class, just re-use it
webView.loadUrl("file:///"+getContext().getFilesDir().toString()+"/index.html");
現在作出這樣的接口:
public class JSInterface {
// these fcns are exposed to the WebView
private Context context;
public JSInterface(Context context)
{
context = context;
}
@JavascriptInterface
public void doEchoTest(String echo)
{
//A useful test; I usually use it in a webViewClient that runs the function that calls this in my javascript with this:
//webView.loadUrl("javascript:echo('"echo!!!"');"); //this exact formatting
//but webViewClient is outside the scope of your question and would just confuse the
//issue; however, you may need to implement
//webViewClient with an delaying asynctask to seperatly run the js you loaded with the webpage,
//again, I used to do this ground-up, and phoneGap Might Just Work.
Toast.makeText(context, echo, Toast.LENGTH_SHORT).show();
}
@JavascriptInterface
public void TurnOnTuneIn
{
aMethodThatHasThatFirstBitOfCodeIPublished();
}
}
我愛接口=]
其餘部分完全是爲了你的目的而設計的樣板(儘管如此,玩起來還是有趣的!),如果有的話,你可能不需要太多。如果你注意到我放置頂端的方法沒有按照你想要的方式返回,你可以使用startActivityForResult方法來做同樣的事情,我敢打賭它會很好地工作;請注意'超級'。調用Override進行;你只需要Intent(就像我向你展示的那樣)和一些參考編號來獲得結果...再次超出問題的範圍。此外,我還包括一個我正在使用它的人員對ResultCallback b/c的支持,但我自己並沒有使用它。
@Override
public Activity getActivity(){
return this;
}
@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
this.activityResultCallback = plugin;
}
public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
this.activityResultCallback = command;
this.activityResultKeepRunning = this.keepRunning;
// If multitasking turned on, then disable it for activities that return results
if (command != null) {
this.keepRunning = false;
}
// Start activity
super.startActivityForResult(intent, requestCode);
}
@Override
public void cancelLoadUrl(){
// no op
}
@Override
public Object onMessage(String id, Object data) {
LOG.d("is", "onMessage(" + id + "," + data + ")");
if ("exit".equals(id)) {
super.finish();
}
return null;
}
@Override
public void onPause() {
Method pause = null; // Pauses the webview.
try {
pause = WebView.class.getMethod("onPause");
}
catch (SecurityException e) { }
catch (NoSuchMethodException e) { }
if (pause != null) {
try { pause.invoke(webView);
}
catch (InvocationTargetException e) { }
catch (IllegalAccessException e) { }
}
else {
// No such method. Stores the current URL.
suspendUrl = webView.getUrl(); // And loads a URL without any processing.
webView.loadUrl("file:///android_asset/nothing.html");
}
super.onPause();
}
@Override
public void onResume() {
super.onResume();
Method resume = null; // Resumes the webview.
try {
resume = WebView.class.getMethod("onResume");
}
catch (SecurityException e) { }
catch (NoSuchMethodException e) { }
if (resume != null) {
try {
resume.invoke(webView);
}
catch (InvocationTargetException e) { }
catch (IllegalAccessException e) { }
}
else if (webView != null) { // No such method. Restores the suspended URL.
if (suspendUrl == null) {
//this should be wherever you have your page; you can probably copy it from what is there now.
webView.loadUrl("file:///"+getContext().getFilesDir().toString()+"/index.html");
}
else {
webView.loadUrl(suspendUrl);
}
}
}
希望能讓你更進一步;如果你沒有使用它,記得使用版本控制,所以你可以魯莽你的編輯!
GL HF
沒有ü添加ACCESS_FINE_LOCATION許可清單文件,當然 –
是的,我有機會獲得用戶的位置,我的問題是一個插件像錯誤 – David
使用打開設置頁面:https://github.com/romainperruchon/ gpssettings/BLOB /主/ src目錄/安卓/ COM/dataforpeople /插件/ GpsSettings.java。 –