public class SMSWidget extends AppWidgetProvider {
private static final String queryString = "@inpion";
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
//get SMS Message
if(intent.getAction().equals(SMS_RECEIVED)){
Bundle bundle = intent.getExtras();
if(bundle != null){
Object[] pdus = (Object[])bundle.get("pdus");
int pduslen = pdus.length;
SmsMessage[] messages = new SmsMessage[pduslen];
String str= "";
for(int i=0; i<pduslen; i++){
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
try {
str += new String(messages[i].getMessageBody().getBytes(),"GB2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(SmsMessage message : messages){
String msg = message.getMessageBody();
Log.d("======messages=", messages.toString());
if(msg.toLowerCase().startsWith(queryString)){
Log.d("======SMS RECIVED======", "OK");
//View new message
RemoteViews rv ;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName smsWidget = new ComponentName( context, SMSWidget.class );
rv = new RemoteViews(context.getPackageName(), R.layout.main);
rv.setTextViewText(R.id.catch_sms, "msg:"+msg);
appWidgetManager.updateAppWidget(smsWidget, rv );
//Delete SMS
try{
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(uriSms, null, null, null, null);
if(c != null ){
//do{
int threadId = c.getInt(1);
Log.d("===threadId: ",threadId+"");
int a = context.getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadId), null, null);
Log.d("===a: ", a+"");
//}while (c.moveToNext());
}else{
Log.d("=no=", c.toString());
}
}catch(Exception e){
e.printStackTrace();
}}}}
android.xml :
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".SMSWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/catch_sms_widget_provider" />
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-sdk android:minSdkVersion="5" />
在我的应用程序中,我发送短信但在主屏幕上,AppWidget无法接收它,只有通知可以通知我,我想在我的AppWidget中收到的消息frist没有出现在通知栏中,谢谢?
答案 0 :(得分:0)
在我的代码msg.toLowerCase()。startsWith(queryString)canot运行,所以我删除它,同时在xml中 intent-filter android:priority =“1”,我修改android:priority =“20”,在“删除短信”部分,我没有意识到临时