0

我正在尝试为我为学习目的而创建的消息传递应用程序设置一个简单的呼叫者 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();
    }
}

}

任何指向我正确方向的提示将不胜感激。我已经为此工作了几天,这让我发疯。提前致谢。

4

1 回答 1

0

您需要一个具体MainActivity 实例来调用updateMessageBox().

当接收到广播时,以太开始一个新的活动(或显示通知、吐司或其他),或者您的活动应该在其方法中注册一个接收者onResume(),并在其方法中取消注册它onPause()。第一种方式意味着您的应用程序接收所有 SMS 消息,第二种方式意味着当用户不在您的活动中时消息将被忽略。您希望选择哪种方法取决于您的应用程序。

特别是对于第二个选项,请参阅有关活动中的 BroadcastRecivers 的相关问题

于 2013-08-11T20:15:09.427 回答