我是 android 开发的新手,我正在创建一个应用程序,它接收格式为“获取名称”的短信,名称是地址簿中的联系人,并将名片返回给发件人。我已经添加了所有权限,但是每当我发送消息“获取名称”时它似乎崩溃了。尽管不涉及函数调用,但它确实适用于无效命令
这是我的主要活动
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
这是广播接收器
public class SmsReceiver extends BroadcastReceiver
{
String str="",phoneno="";
MainActivity object=new MainActivity();
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
.
.
.
//---retrieve the SMS message received---
.
.
.
if(command.equalsIgnoreCase("get")){
try{
Intent start_contact_retrieve=new Intent();
start_contact_retrieve.setClass(context, ContactRetrieve.class);
context.startService(start_contact_retrieve);
context.stopService(start_contact_retrieve);
}
catch(Exception e){
Log.e("receiver function", e.getMessage());
}
//---display the new SMS message---
.
.
.
}
else{
SmsManager invalid_command=SmsManager.getDefault();
invalid_command.sendTextMessage(phoneno, null, "Invalid Command", null, null);
}
}
}
这就是我要调用的服务
public class ContactRetrieve extends Service{
SmsReceiver sreceiver=new SmsReceiver();
String argument_name=sreceiver.str;
String argument_phoneno=sreceiver.phoneno;
@Override
public IBinder onBind(Intent arg0) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void onCreate() {
super.onCreate();
String returned_data=retrievecontact();
SmsManager smsman= SmsManager.getDefault();
smsman.sendTextMessage(argument_phoneno, null, returned_data, null, null);
}
public String retrievecontact(){
String phone="No Contact Found";
ContentResolver cr=getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
.
.
.
.
}
}
return phone;
}
}
我尝试在主要活动本身中调用此函数,但它也不起作用。该程序只是停止响应。在过去的两天里,我一直被这个问题困扰。任何帮助将不胜感激。PS对不起所有的代码。
这是更新的代码....
@Override
public void onCreate() {
super.onCreate();
Thread t=new Thread(this);
t.start();
}
public String retrievecontact(){
String phone="No Contact Found";
try{
ContentResolver cr=getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
.
.
.
}
}
}
}
catch(Exception e){
Log.e("ERROR", e.getMessage());
}
return phone;
}
public void run() {
String returned_data=retrievecontact();
SmsManager smsman= SmsManager.getDefault();
smsman.sendTextMessage(argument_phoneno, null, returned_data, null, null);
stopSelf();
}
日志猫
??-?? ??:??:??.??? 0 INFO <unknown> [ 11-05 14:04:04.204 1571: 1585 E/AndroidRuntime ]
??-?? ??:??:??.??? 0 INFO <unknown> FATAL EXCEPTION: Thread-144
??-?? ??:??:??.??? 0 INFO <unknown> at android.minor_project.ContactRetrieve.run(ContactRetrieve.java:172)
??-?? ??:??:??.??? 0 INFO <unknown> [ 11-05 14:04:04.324 190: 291 W/ActivityManager ]
??-?? ??:??:??.??? 0 INFO <unknown> [ 11-05 14:04:04.343 190: 291 W/WindowManager ]