我正在尝试在后台短信服务收到特定发件人sms时启动应用程序。我已经编写了msg接收部分,但是当msg receive.it需要启动地图位置活动时我的应用程序无法启动。
我的msgrecieve课程
public class MsgRecieve extends Service {
public class MsgReciever extends BroadcastReceiver {
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
if (senderNum.equals("956xxxxxxx")) {
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
toast.show()
/*Intent i1 = new Intent(context,MapsActivity.class);
i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i1);*/
}
} // end for loop
} // bundle is null
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的活动课
try {
super.onCreate(savedInstanceState);
setApplicationContext();
setContentView(R.layout.activity_main);
Intent intent1 =new Intent(this,MsgRecieve.class);
startService(intent1);
TextView txtImei1 = (TextView) findViewById(R.id.imei1);
TextView txtImei2 = (TextView) findViewById(R.id.imei2);
TextView txtSerial = (TextView) findViewById(R.id.serialNo);
TextView txtAlert = (TextView) findViewById(R.id.alertMsg);
IMEIHelper imeiHelper = new IMEIHelper();
TelephonyInfo telephonyInfo = imeiHelper.getInfo(getApplicationContext());
if (telephonyInfo.isSIM1Ready()) {
txtImei1.setText(StringHelper.IMEI + "1:" + telephonyInfo.getImeiSIM1());
} else {
txtImei1.setText(StringHelper.IMEI + ":NA");
}
if (telephonyInfo.isSIM2Ready()) {
txtImei2.setText(StringHelper.IMEI + "2:" + telephonyInfo.getImeiSIM2());
}
清单
<service
android:name="edu.am.edu.am.iotm.android.activity.MsgRecieve"
android:enabled="true"
android:exported="true" >
</service>
</application>
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<!--The ACCESS_COARSE/FINE_LOCATION permissions are not required to useGoogle Maps Android API v2, but are recommended.-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<intent-filter android:priority="901" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
答案 0 :(得分:0)
清单:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
广播接收器类:
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
/* Start a new application*/
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("application package name");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) { }
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
}
}
修改强>
试试这个,我已经在你messagereceive class
进行了编辑:
public class MsgRecieve extends Service {
public class MsgReciever extends BroadcastReceiver {
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
if (senderNum.equals("956xxxxxxx")) {
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "senderNum: " + senderNum + ", message: " + message, duration);
toast.show()
Intent mIntent;
PackageManager manager = getPackageManager();
try {
mIntent = manager.getLaunchIntentForPackage("com.application.package.name");
if (mIntent == null)
throw new PackageManager.NameNotFoundException();
mIntent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(mIntent);
} catch (PackageManager.NameNotFoundException e) { }
}
} // end for loop
} // bundle is null
} catch (Exception e) {
e.printStackTrace();
}
}
}