forked from hussien89aa/AndroidTutorialForBeginners
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BroadcastReceiver.java
76 lines (59 loc) · 2.83 KB
/
BroadcastReceiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Define BroadcastReceiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// get the bundles in the message
final Bundle bundle = intent.getExtras();
// check the action equal to the action we fire in broadcast,
if ( intent.getAction().equalsIgnoreCase("com.example.Broadcast"))
//read the data from the intent
Toast.makeText(context,bundle.getString("username"),Toast.LENGTH_LONG).show();
if ( intent.getAction().equalsIgnoreCase("android.provider.Telephony.SMS_RECEIVED"))
{
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i < messages.length; i++) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String format = bundle.getString("format");
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i], format);
}
else {
messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
}
// SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String senderNum =messages[i].getOriginatingAddress();
String message =messages[i].getMessageBody();//
}
}
}
/* add to AndroidManifest.xml
<uses-permission android:name="android.permission.READ_SMS" />
<receiver android:name=".MyReceiver"
android:permission="android.permission.BROADCAST_SMS"
tools:ignore="UnprotectedSMSBroadcastReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="com.example.Broadcast" >
</action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
*/
// send sendBroadcast
Intent intent = new Intent();
//set the action that will receive our broadcast
intent.setAction("com.example.Broadcast");
// add data to the bundle
intent.putExtra("username", "alxs1aa");
// send the data to broadcast
sendBroadcast(intent);
//send sms
SmsManager smsManagersend = SmsManager.getDefault();
smsManagersend.sendTextMessage("phone number", null,"hellor from android", null, null);
//start activity from
//start activity
Intent i = new Intent(context,MainActivity.class);
// i.setClassName("com.test", "com.test.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);