使用HTML5(提醒应用程序)访问电话报警(本地资源) [英] Access phone alarm (native resources)using html5 (Reminder app)

查看:426
本文介绍了使用HTML5(提醒应用程序)访问电话报警(本地资源)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些都是我的技术,

  1. 在HTML5
  2. 在jQuery Mobile的,jQuery的,Java脚本
  3. 的CSS
  4. 在科尔多瓦

我们正在开发使用HTML 5应用程序,我们需要访问手机(Android设备,iPhone,Windows phone)系统资源,如报警(创建提示)。 问题,

We are developing app using HTML 5 and we need to access phone (Android,iPhone,Windows Phone) resources, such as alarm (to create reminder). Questions,

  1. 我们能否通过这一技术进入手机本地资源?
  2. 我们需要不同的编码来访问不同的移动操作系统?
  3. 什么是这种方法的优点和缺点是什么?
  4. 什么是这样做的最佳方法是什么?
  5. 有什么建议?
  1. Can we access native resources of mobile phones through this technologies ?
  2. Do we need different coding to access different mobile operating systems ?
  3. What are the pros and cons of this method ?
  4. what is the Best method of doing this ?
  5. Any suggestions ?

通过JavaScript样品code或实例访问Android设备,iPhone和Windows Phone 8报警经理吗?

Sample code or examples for access Android, Iphone and Windows phone 8 alarm manager through javascript?

没有任何一个使用此后台服务插件完成这个任务?

Did any one use this Background Service Plugin for this task?


我问过关于这个用于接入报警经理
问题
做任何一个有样本code或一步的指导给我的一个步骤,如何使用这个访问Android的报警管理器?


I asked question regarding this use for access alarm manager

do any one have sample code or give me a step by step guidance, how to use this for access Android alarm manager ?

我试图通过像<一个Java脚本来访问本地code href="http://stackoverflow.com/questions/2727763/communication-between-android-java-and-phonegap-javascript/22014440#22014440">this一问一答,但它不工作。

I try to access native code through java script like this question and answer but it's not working.

当我点击该按钮;

  1. 在Eclipse中logcat的;在科尔多瓦标签

未捕获的类型错误:对象[对象的对象]有没有办法getTelephoneNumber

Uncaught TypeError:Object [object Object] has no method 'getTelephoneNumber'

  1. 在Eclipse中logcat的;在铬标记,

[INDO:控制台(22)]未捕获的类型错误:对象[对象的对象]有没有办法'getTelephoneNumber',来源:文件:///android_asset/www/index.html(22)

[INDO:CONSOLE(22)]"Uncaught TypeError:Object [object Object] has no method 'getTelephoneNumber'", source: file:///android_asset/www/index.html (22)

我PhoneGap的版本是2.7.0和仿真器API级别19是什么问题在这里?我错过了什么?

my phonegap version is 2.7.0 and Emulator API level 19. what is the problem in here ? did i miss anything ?

推荐答案

一个长期运行的过程中,我设法想出的解决了Android设备后。照片 使用的JavaScript 1)访问本地code是可能的
2)使用JavaScript接口提高XSS漏洞的威胁,但在最新的API等级19淘汰这一点。 (阅读更多...)
让code说话; MainActivity code首先

After a long running process i manage to come up with solution to the android devices.
1) Access native code using javascript is possible
2) Using javascript interface raise a threat of xss vulnerability, but in Latest API Level 19 eliminated this. (Read more...)
Let the code speak; MainActivity code first

@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
public class MainActivity extends DroidGap

1)onCreate方法code

1) onCreate method code

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);

2)为了完成你需要有提醒类在你的MainActivity类什么都html文件,在你的资产文件夹下一个步骤。就我而言,我有 的login.html 资产/网络/手机/ login.html的

2) To complete next step you need to have Reminder class with in your MainActivity class and what ever html file in your assets folder. In my case i have Login.html in 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);

3)这是我的提示类它的构造

    public class Reminder  {        
    private Context con;        
    public Reminder(Context con){
        this.con=con;
    }
   }

4) 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();
    // end of toast...

    // Show status bar notification
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(receiverID, mBuilder.build());

}}

5)的方法,可以通过JavaScript调用,并负责添加报警对象为系统报警管理。在提醒类

5) Method that can call through JavaScript and responsible to add Alarm object into System Alarm Manager. In Reminder class

@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=>as 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);                    

            }

6)现在,你需要编辑你的Andr​​oid的Manifest.xml
设置权限,

6) Now, you need to edit your Android Manifest.xml
set permission,

<uses-permission android:name="android.permission.VIBRATE" />

和注册ReminderReceiver类,在标签中添加这一行(里面没有任何其他标​​记!)

And Register ReminderReceiver class, add this lines with in tag (not inside of any other tags !!!),

<receiver android:name=".ReminderReceiver">
 </receiver>

7)最后,在你的HTML文件中添加按钮,添加JavaScript函数,调用它的按钮单击事件和函数调用内部的 addReminder 方式

 function test() {            

         Reminder.addReminder(2014,3,4,12,30);

     }

希望这个答案将有助于谁试图使用Android手机的原生功能,同时通过PhoneGap的发展

这篇关于使用HTML5(提醒应用程序)访问电话报警(本地资源)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆