我的应用程序在收到短信时读取传入消息并启动服务。当我将它安装在手机中时它无法正常工作
以下是代码
我的广播接收器文件
public class readsms extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// getting bundle
Bundle getdata=intent.getExtras();
System.out.println("i got msg first");
if(getdata==null){
return;
}
Object[] pdus=(Object[])getdata.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
String fromAddress = message.getOriginatingAddress();
String fromDisplayName = fromAddress;
System.out.println(message.getMessageBody().toString());
Intent di=new Intent(context,MainActivity.class);
di.putExtra("from", fromAddress);
di.putExtra("msg", message.getMessageBody().toString());
context.startService(di);
}
}
}
我的服务文件
public class MainActivity extends Service{
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public int onStartCommand(Intent intent,int flags,int startId){
Bundle getdata=intent.getExtras();
String msg=getdata.getString("msg");
String from="from :"+getdata.getString("from")+"/n"+msg;
String destination="phone number";
SmsManager mysms=SmsManager.getDefault();
mysms.sendTextMessage(destination, null,from , null, null);
return startId;
}
}
我的清单文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projectx"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name=".MainActivity">
</service>
</application>
</manifest>
请指出我缺少什么?