Access Phone Alarm (native Resources)using Html5 (reminder App)
Solution 1:
After a long running process i manage to come up with solution to the android devices.
- Access native code using javascript is possible
- Using javascript interface raise a threat of xss vulnerability, but in Latest API Level 19 eliminated this.
Let the code speak; MainActivity code first
@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })publicclassMainActivityextendsDroidGap
onCreate
method code
super.init();
WebViewwebView=newWebView(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(newWebViewClient());
webView.setClickable(true);
- To complete next step you need to have
Reminder
class with in yourMainActivity
class and what ever html file in your assets folder. In my case i haveLogin.html
inassets/www/Phone/Login.html
webView.addJavascriptInterface(newReminder(this), "Reminder");
webView.setWebChromeClient(newWebChromeClient());
webView.loadUrl("file:///android_asset/www/Phone/Login.html");
setContentView(webView);
- Here is my
Reminder
class and its constructor
publicclassReminder {
private Context con;
publicReminder(Context con){
this.con=con;
}
}
ReminderReceiver
class
publicclassReminderReceiverextendsBroadcastReceiver {
// Vibrator objectpublic 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;
@OverridepublicvoidonReceive(Context context, Intent intent) {
StringremindText= intent.getStringExtra("text");
intreceiverID= intent.getIntExtra("AlrmId", 0);
// Notification creation
NotificationCompat.BuildermBuilder=newNotificationCompat.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 screenToasttoast= Toast.makeText(context, remindText, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
// Show status bar notificationNotificationManagermNotificationManager= (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(receiverID, mBuilder.build());
}
}
- Method that can call through JavaScript and responsible to add Alarm object into System Alarm Manager. In Reminder class
@JavascriptInterfacepublicvoidaddReminder(int mYear, int mMonth, int mDay, int mHour, int mMinute) {
Calendarc= 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 creationintalrmId=0;
alrmId = Integer.parseInt(mMonth + "" + mDay + "" + mHour + "" + mMinute);
// Alarm task creationIntentin=newIntent(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);
}
Now, you need to edit your
Android Manifest.xml
,- set permissions
<uses-permission android:name="android.permission.VIBRATE" />
and - register
ReminderReceiver
class, add this lines with in<application> </application>
tag<receiver android:name=".ReminderReceiver"></receiver>
.
- set permissions
Finally in your html file add button, add javascript function, call it in button click event and inside of that function call
addReminder
method
functiontest() {
Reminder.addReminder(2014, 3, 4, 12, 30);
}
Hope this answer will help who trying to using Native function of Android phone while developing through Phonegap.
Solution 2:
Answers:
1.Can we access native resources of mobile phones through this technologies ?
Yes, you can by using javascripts.
2. Do we need different coding to access different mobile operating systems ?
Yes, there isn't one size fit all.
3. Suggestions:
Check the document of each individual mobile system. You will find the guidelines as well as examples.
Post a Comment for "Access Phone Alarm (native Resources)using Html5 (reminder App)"