这些是我的技术,
- HTML5
- Jquery 移动版、Jquery、Java 脚本
- CSS
- Cordova
我们正在使用 HTML 5 开发应用程序,我们需要访问手机(Android、iPhone、Windows Phone)资源,例如闹钟(以创建提醒)。 问题,
- 我们可以通过这种技术访问手机原生资源吗?
- 我们是否需要不同的编码来访问不同的移动操作系统?
- 这种方法的优缺点是什么?
- 执行此操作的最佳方法是什么?
- 有什么建议吗?
通过 javascript 访问 Android、Iphone 和 Windows phone 8 警报管理器的示例代码或示例?
有没有人用过这个Background Service Plugin为了这个任务?
我问了关于这个的问题 use for access alarm manager
有没有人有示例代码或给我一步一步的指导,如何使用它来访问 Android 闹钟管理器?
我尝试通过 this question and answer 之类的 java 脚本访问 native 代码但它不工作。
当我点击按钮时;
- 在 eclipse logcat 中;在 cordova 标签下,
Uncaught TypeError:Object [object Object] 没有方法 'getTelephoneNumber'
- 在 eclipse logcat 中;在 Chrome 标签下,
[INDO:CONSOLE(22)]“Uncaught TypeError:Object [object Object] 没有方法‘getTelephoneNumber’”,来源:file:///android_asset/www/index.html (22)
我的 phonegap 版本是 2.7.0 和 Emulator API 级别 19。这里有什么问题?我错过了什么吗?
最佳答案
经过长时间的运行,我设法找到了适用于 Android 设备的解决方案。
- 使用 javascript is possible 访问 native 代码
- 使用 javascript 接口(interface)会引发 xss 漏洞的威胁,但在最新的 API 级别 19 中消除了这一点。
让代码说话;MainActivity代码先行
@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
public class MainActivity extends DroidGap
-
onCreate
方法代码
super.init();
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
// Add these lines according to your requirements
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setSaveFormData(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
//webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setSupportZoom(true);
webView.setWebViewClient(new WebViewClient());
webView.setClickable(true);
- 要完成下一步,您需要
Reminder
在你的课上MainActivity
类以及 Assets 文件夹中的任何 html 文件。就我而言,我有Login.html
在assets/www/Phone/Login.html
webView.addJavascriptInterface(new Reminder(this), "Reminder");
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_asset/www/Phone/Login.html");
setContentView(webView);
- 这是我的
Reminder
类及其构造函数
public class Reminder {
private Context con;
public Reminder(Context con){
this.con=con;
}
}
-
ReminderReceiver
类
public class ReminderReceiver extends BroadcastReceiver {
// Vibrator object
public Vibrator vibrator;
long[] pattern = { 0L, 250L, 200L, 250L, 200L, 250L, 200L, 250L, 200L, 250L, 200L, 250L, 200L, 250L, 200L };
// Ringtone object
Uri ringT;
Ringtone ringTone;
@Override
public void onReceive(Context context, Intent intent) {
String remindText = intent.getStringExtra("text");
int receiverID = intent.getIntExtra("AlrmId", 0);
// Notification creation
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.ic_popup_reminder)
.setContentTitle("Reminder").setContentText(remindText);
// Create vibrator pattern
vibrator = (Vibrator) context
.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern, -1);// No repetition
// Notification tone creation and play
ringT = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
ringTone = RingtoneManager.getRingtone(context, ringT);
ringTone.play();
// Create toast and show on center of the screen
Toast toast = Toast.makeText(context, remindText, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
// Show status bar notification
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(receiverID, mBuilder.build());
}
}
- 可以通过 JavaScript 调用并负责将警报对象添加到系统警报管理器中的方法。在提醒类
@JavascriptInterface
public void addReminder(int mYear, int mMonth, int mDay, int mHour, int mMinute) {
Calendar c = Calendar.getInstance();
// set Reminder time and date into calendar object
c.set(Calendar.YEAR, mYear);
c.set(Calendar.MONTH, mMonth); // Don't use exact numeric value of the month, use one minus. Ex: April => 3
c.set(Calendar.DATE, mDay);
c.set(Calendar.HOUR_OF_DAY, mHour);
c.set(Calendar.MINUTE, mMinute);
c.set(Calendar.SECOND, 0);
// Unique Alarm ID creation
int alrmId = 0;
alrmId = Integer.parseInt(mMonth + "" + mDay + "" + mHour + "" + mMinute);
// Alarm task creation
Intent in = new Intent(con, ReminderReceiver.class);
in.putExtra("text", "You have a Reminder!");
in.putExtra("AlrmId", alrmId);
PendingIntent pi;
pi = PendingIntent.getBroadcast(con, alrmId, in, 0);
AlarmManager am;
am = (AlarmManager) (con.getSystemService(Context.ALARM_SERVICE));
am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
}
现在,您需要编辑您的
Android Manifest.xml
,- 设置权限
<uses-permission android:name="android.permission.VIBRATE" />
和 - 注册
ReminderReceiver
类,在<application> </application>
中添加此行标签<receiver android:name=".ReminderReceiver"></receiver>
.
- 设置权限
最后在您的 html 文件中添加按钮,添加 javascript 函数,在按钮单击事件中调用它并在该函数调用内部
addReminder
方法
function test() {
Reminder.addReminder(2014, 3, 4, 12, 30);
}
希望这个答案能帮助那些在通过 Phonegap 开发时尝试使用 Android 手机的 Native 功能的人。
关于android - 使用 html5(提醒应用程序)访问电话警报( native 资源),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21826170/