PDA

View Full Version : [android] programmare app per gestione sms aiuto


atelzut2
16-08-2014, 19:21
ciao a tutti mi sto avventurando nel mondo delle sms-app di android.
leggendo qua e la su internet ho trovato un po di codice che no deve far altro che visualizzare un toast se arriva un sms. nulla di che se non fosse che non va. sopno sicuro sia colpa mia in quanto ho commesso errori sicuro stupidi.
mi potresti dare una mano?
qui il codice.


il broadcastreceiver

package com.example.mysmsreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;

import android.util.Log;
import android.widget.Toast;

public class IncomingSms extends BroadcastReceiver{



// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();

try {

if (bundle != null) {

final Object[] pdusObj = (Object[]) bundle.get("pdus");

for (int i = 0; i < pdusObj.length; i++) {

SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();

String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();

Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);


// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();

} // end for loop
} // bundle is null

} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);

}
}
}




il manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.secure.sms"
android:versionCode="1"
android:versionName="0.1" >

<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

<application android:icon="@drawable/icon"
android:label="@string/app_name" >

<activity android:name=".SecureMessagesActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver android:name=".SmsReceiver" android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

</application>

</manifest>

activity_main
package com.example.mysmsreceiver;

import android.support.v7.app.ActionBarActivity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter( "android.provider.Telephony.SMS_RECEIVED" );
filter.setPriority( IntentFilter.SYSTEM_HIGH_PRIORITY );
registerReceiver( new IncomingSms(), filter );
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


grazie mille a chiunque trovi un motivo per dirmi che non son capace. :-)

<Gabrik>
17-08-2014, 18:19
cosė da una prima occhiata, non hai bisogno di registrare il Broadcast Receiver 2 volte (una nel manifest e una nel onCreate dell'Activity) basta quella del manifest.

che comunque dovrebbe cosė (non ne sono sicuro al 100%, non ho mai provato a registrarmi per l'Intent SMS_RECEIVED)


<receiver
android:name=".SmsReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

atelzut2
27-08-2014, 23:17
Eccolo la. Il solito errore scemo. Grazie mille