I am new to android, and I am trying to learn by doing some basics projects. The current app I working on requires me to use BroadcastReceiver, but I need it to run when the phone starts and when the app closes.
In need it to receive all the incoming SMS, even when the app is not running.
Please help.
Here is my code.
public class IncomingSMS extends BroadcastReceiver {
// Get the object of SmsManager
final SmsManager sms = SmsManager.getDefault();
//get audioManager
AudioManager aud;
@Override
public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
//Log.i("MyActivity", "aaa");
final Bundle bundle = intent.getExtras();
aud = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
try {
if (bundle != null) {
Log.i("MyActivity", "Bundle");
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String message = currentMessage.getDisplayMessageBody();
Log.i("MyActivity", message);
................
} // end for loop
} // bundle is null
}catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
Log.i("MyActivity", "bbb");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
package="......." >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="-----MainActivity"
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="......IncomingSMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|s creenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
No comments:
Post a Comment