我做了一个锁定屏幕的小应用程序。但这只有在我打开应用程序并单击我的锁定按钮时才会发生。
我想通过发送TextMessage来做到这一点。那就是如果我从预先存储的号码发送一个txt“Lock”它应该做我的按钮OnClick Listener的wokr
任何人都可以指导我如何开始吗?
成为Android的新手我不知道如何开始使用这个
答案 0 :(得分:0)
试用此代码:
public class SmsReceiver extends BroadcastReceiver
{
String message ;
boolean check_message ;
String senderNUM ;
static String H ;
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
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;
senderNUM = senderNum ;
message = currentMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);
H = currentMessage.getMessageBody();
// Show Alert
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,
"senderNum: "+ senderNum + ", message: " + message, duration);
toast.show();
if(i==0)
H = currentMessage.getMessageBody();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" +e);
}
check_message = message_checked(H) ;
//---display the new SMS message---
if(check_message)
{
abortBroadcast();
Intent intentHome = new Intent(context,MainActivity.class);
intentHome.putExtra("msgContent", message);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentHome);
}
else
System.exit(0);
}
public boolean message_checked(String the_message)
{
String Code = new String("*%#&");
String four_char = "" ;
for(int i = 0 ; i<4 ; i++)
{
four_char += the_message.charAt(i) ;
}
if(four_char.equals(Code))
{
return true;
}
else
return false ;
}
}