android 从网络界面发送短信
android send sms from webinterface
我正在使用 webview 构建一个 android webapp,并添加了 webinterface class。
这是我的代码:
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
public void SendSMS(String msg,String PhoneNumber) {
Toast.makeText(mContext,
"sending",
Toast.LENGTH_LONG).show();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(PhoneNumber, null, msg, null, null);
} catch(Exception e) {
Toast.makeText(mContext,
"SMS not sent, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
主要活动:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
if (checkconnection()) {
myWebView.loadUrl("file:///android_asset/www/index.html");
} else {
Context context = getApplicationContext();
CharSequence text = "אין חיבור לאינטרנט";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean checkconnection() {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
} else {
return false;
}
}
}
表格Javascript我叫
Android.showToast('test')
<======= 这个很好用
Android.SendSMS('0587070580','test sms')
<========== 不工作
没有异常。
您的 SendSMS()
方法缺少 @JavascriptInterface
注释。
Starting from API level JELLY_BEAN_MR1 and above, only methods explicitly marked with this annotation are available to the Javascript code.
我正在使用 webview 构建一个 android webapp,并添加了 webinterface class。
这是我的代码:
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
public void SendSMS(String msg,String PhoneNumber) {
Toast.makeText(mContext,
"sending",
Toast.LENGTH_LONG).show();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(PhoneNumber, null, msg, null, null);
} catch(Exception e) {
Toast.makeText(mContext,
"SMS not sent, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
主要活动:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
if (checkconnection()) {
myWebView.loadUrl("file:///android_asset/www/index.html");
} else {
Context context = getApplicationContext();
CharSequence text = "אין חיבור לאינטרנט";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean checkconnection() {
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
return true;
} else {
return false;
}
}
}
表格Javascript我叫
Android.showToast('test')
<======= 这个很好用
Android.SendSMS('0587070580','test sms')
<========== 不工作
没有异常。
您的 SendSMS()
方法缺少 @JavascriptInterface
注释。
Starting from API level JELLY_BEAN_MR1 and above, only methods explicitly marked with this annotation are available to the Javascript code.