added SMS receive functionality

This commit is contained in:
rnvahey@gmail.com 2009-02-24 22:45:42 +07:00
parent 294694ef5d
commit 4993c1bf87
29 changed files with 161 additions and 28 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -12,6 +12,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">

View File

@ -4,7 +4,7 @@
* website: http://phonegap.com
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* Software), to deal in the Software without restriction, including
* Software), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
@ -13,7 +13,7 @@
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
@ -284,16 +284,24 @@ var Device = {
return Device.exec("vibrate")
},
playSound: function(clip) {
xsound = "sound:";
if (Device.whatPlatform == "Android") {
xsound = "playSound";
return Device.exec(xsound + clip);
}
return Device.exec(xsound + clip);
}
playSound: function(clip) {
xsound = "sound:";
if (Device.whatPlatform == "Android") {
xsound = "playSound";
return Device.exec(xsound + clip);
}
return Device.exec(xsound + clip);
},
// notifications
notification: {
watchPosition: function(filter) {
window.DroidGap.notificationWatchPosition(filter);
},
clearWatch: function(filter) {
window.DroidGap.notificationClearWatch(filter);
}
}
}
function gotLocation(lat, lon) {

View File

@ -15,11 +15,11 @@
initGap = function() {
// Device.init() initializes OS-Device values
Device.init();
// UNDO Device.init();
// Device.Location.init() to initalize location
Device.Location.init();
// UNDO Device.Location.init();
//Device.Acceleration.callback = updateAccel;
}
@ -86,7 +86,24 @@ updateAccel = function(){
}
addLoadEvent(initGap);
receiveSmsNotification = function(func)
{
if (func == 'watchPosition') {
Device.notification.watchPosition('SMS');
} else
if (func == 'clearWatch') {
Device.notification.clearWatch('SMS');
}
}
function onReceiveSms(number, message)
{
$('number').value = number;
$('message').value = message
}
// UNDO addLoadEvent(initGap);
</script>
</head><body orient="landscape">
@ -105,6 +122,7 @@ addLoadEvent(initGap);
<li><a href="#vibration" onclick="Device.vibrate();">Vibration</a></li>
<li><a href="#vibration" onclick="Device.playSound('on.mp3")">Play Sound</a></li>
<li><a href="#photo" onclick="Device.Image.getFromPhotoLibrary();">Photo...</a></li>
<li><a href="#notification" onclick="notification();">Notification...</a></li>
<li><a href="http://phonegap.com/" target="_self">About</a></li>
</ul>
@ -218,6 +236,26 @@ addLoadEvent(initGap);
</fieldset>
</div>
<div id="notification" title="notification" class="panel">
<h2>Notification</h2>
<fieldset>
<div class="row">
<a class="button leftButton" type="submit" onclick="receiveSmsNotification('watchPosition');">startSmsListener</a>
<a class="button blueButton" type="submit" onclick="receiveSmsNotification('clearWatch');">stopSmsListener</a>
</div>
</fieldset>
<fieldset>
<div class="row">
<label>from:</label>
<input disabled="enabled" id="number" type="text"></input>
</div>
</fieldset>
<fieldset>
<div class="row">
<label>message:</label>
<input disabled="enabled" id="message" type="text"></input>
</div>
</fieldset>
</div>
<div id="preloader"></div></body></html>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -4,7 +4,7 @@ package com.nitobi.phonegap;
* website: http://phonegap.com
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* Software), to deal in the Software without restriction, including
* Software), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
@ -13,7 +13,7 @@ package com.nitobi.phonegap;
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
@ -24,17 +24,14 @@ package com.nitobi.phonegap;
import java.io.IOException;
import android.content.Context;
import android.content.IntentFilter;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.webkit.WebView;
public class PhoneGap{
@ -51,6 +48,7 @@ public class PhoneGap{
private GpsListener mGps;
private NetworkListener mNetwork;
protected LocationProvider provider;
SmsListener mSmsListener;
public PhoneGap(Context ctx, Handler handler, WebView appView) {
this.mCtx = ctx;
@ -58,6 +56,7 @@ public class PhoneGap{
this.mAppView = appView;
mGps = new GpsListener(ctx);
mNetwork = new NetworkListener(ctx);
mSmsListener = new SmsListener(ctx,mAppView);
}
public void updateAccel(){
@ -155,11 +154,7 @@ public class PhoneGap{
mp.prepare();
mp.start();
}
//mp.setDataSource("file:///android_asset/" + filename);
//mp.setDataSource("http://ventrix.nsdc.gr/stuff/TERMITES_SKONH.mp3");
mp.prepare();
@ -214,5 +209,29 @@ public class PhoneGap{
return version;
}
public void notificationWatchPosition(String filter)
/**
* Starts the listener for incoming notifications of type filter
* TODO: JavaScript Call backs for success and error handling. More filter types.
*/
{
if (filter.contains("SMS"))
{
IntentFilter mFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
mCtx.registerReceiver(mSmsListener,mFilter);
}
}
public void notificationClearWatch(String filter)
/**
* Stops the listener for incoming notifications of type filter
* TODO: JavaScript Call backs for success and error handling
*/
{
if (filter.contains("SMS"))
{
mCtx.unregisterReceiver(mSmsListener);
}
}
}

View File

@ -0,0 +1,67 @@
package com.nitobi.phonegap;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.webkit.WebView;
public class SmsListener extends BroadcastReceiver
{
private WebView mAppView;
public SmsListener(Context ctx, WebView mAppView)
{
this.mAppView = mAppView;
}
String ACTION = "android.provider.Telephony.SMS_RECEIVED";
public void onReceive(Context ctx, Intent intent)
{
SmsMessage[] msg;
if (intent.getAction().equals(ACTION))
{
msg = getMessagesFromIntent(intent);
String smsContent = null;
String sendersNumber = null;
for(int i=0; i < msg.length; i++)
{
sendersNumber = msg[i].getDisplayOriginatingAddress();
smsContent = msg[i].getDisplayMessageBody();
}
onReceiveSMS(sendersNumber, smsContent);
}
}
public void onReceiveSMS(String sendersNumber, String smsContent)
/**
* Call back to Java Script
*/
{
mAppView.loadUrl("javascript:onReceiveSms('"+sendersNumber+"',"+"'"+ smsContent +"'"+")");
}
private SmsMessage[] getMessagesFromIntent(Intent intent)
{
SmsMessage retMsgs[] = null;
Bundle bdl = intent.getExtras();
try
{
Object pdus[] = (Object [])bdl.get("pdus");
retMsgs = new SmsMessage[pdus.length];
for(int n=0; n < pdus.length; n++)
{
byte[] byteData = (byte[])pdus[n];
retMsgs[n] = SmsMessage.createFromPdu(byteData);
}
} catch(Exception e)
{
Log.e("SMS_getMessagesFromIntent", "fail", e);
}
return retMsgs;
}
}