我正在尝试为我为学习目的而创建的消息传递应用程序设置一个简单的呼叫者 ID。我在使用 ContactsContact 内容提供程序执行此操作时遇到问题。这是问题所在:
我遇到了这个错误:
无法从 MainActivity 类型对非静态方法 updateMessageBox() 进行静态引用
最简单的解决方法是使 updateMessageBox() 成为静态方法。但是,我不能将 updateMessageBox() 设为静态方法,因为它实现了 getContentResolver(),而不能在静态方法中使用。我认为我在理解静态/非静态声明时遇到了一些麻烦(显然)。这是 MainActivity 类和 SmsReceiver 类。我不会包含 Android Manifest 文件,但我会注意到它确实注册了 SmsReceiver 文件以接收 SMS。
主要活动
public class MainActivity extends Activity {
Button btnSendSMS;
public String recipient = "phone Number was here and it worked";
public static String callerId;
public static String message;
public String sender = "Jeff";
static TextView textView;
public static String LOGTAG = "LOGGED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSendSMS = (Button) findViewById(R.id.button1);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
final EditText textInput=(EditText)findViewById(R.id.editText1);
textView=(TextView)findViewById(R.id.textView1);
final String str=textInput.getText().toString();
sendSMS(recipient, str);
textInput.setText("");
textView.append("\n" + sender + ": " + str);
}
});
}
private void sendSMS(String phoneNumber, String message)
{String SENT = "SMS SENT";
String DELIVERED = "SMS DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,new
Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS DELIVERED",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "GENERIC FAILURE",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "NO SERVICE",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "NULL PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "RADIO OFF",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS Delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS Not Delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
public void updateMessageBox()
{
Cursor contactLookupCursor =
getContentResolver().query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(callerId)),
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
try {
while(contactLookupCursor.moveToNext()){
String contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
String contactId = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
Log.d(LOGTAG, "contactMatch name: " + contactName);
Log.d(LOGTAG, "contactMatch id: " + contactId);
if (contactName != null) {
callerId = contactName;
}
}
} finally {
textView.append("\n" + callerId + ": " + message);
contactLookupCursor.close();
}
}
}
和 SmsReceiver 文件
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle=intent.getExtras();
Object[] messages=(Object[])bundle.get("pdus");
SmsMessage[] sms=new SmsMessage[messages.length];
for(int n=0;n<messages.length;n++){
sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
}
for(SmsMessage msg:sms){
MainActivity.callerId = msg.getOriginatingAddress();
MainActivity.message = msg.getMessageBody();
MainActivity.updateMessageBox();
}
}
}
任何指向我正确方向的提示将不胜感激。我已经为此工作了几天,这让我发疯。提前致谢。