Android - 从另一个类调用方法

时间:2014-12-24 06:52:31

标签: java android android-intent call httpurlconnection

我有两个课程,MainActivity.javaSMSMonitorService.java

收到短信后,我想在mySearch()中致电MainActivity class

MainActivity.java:

//public void myClickHandler(View view) {
public void mySearch() {

    // Gets the URL from the UI's text field.
    String stringUrl = endPointES + urlUser + "&redirects&continue=";
    //String stringUrl = urlText.getText().toString();
    ConnectivityManager connMgr = (ConnectivityManager)
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        new DownloadWebpageTask().execute(stringUrl);

    } else {
        textView.setText("No network connection available.");
        Toast.makeText(getApplicationContext(), "No network connection available", Toast.LENGTH_LONG).show();
    }
}

SMSMonitorService.java:

private final BroadcastReceiver receiver1 = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.i(TAG, "WORKS");
        if (action.equals("android.provider.Telephony.SMS_RECEIVED")) {
            Log.i(TAG, "SMS Broadcast Received");
            Bundle bundle = intent.getExtras();
            SmsMessage[] messages = null;
            String message = "";
            String second = "";

            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                messages = new SmsMessage[pdus.length];
                for (int i = 0; i < messages.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    if (messages[i].getMessageBody().contains("W ") || messages[i].getMessageBody().contains("w ")) {
                        message = messages[i].getMessageBody();
                        StringTokenizer tokens = new StringTokenizer(message, " ");
                        String first = tokens.nextToken();
                        second = tokens.nextToken();
                        number = messages[i].getOriginatingAddress();
                        Toast.makeText(context, second, Toast.LENGTH_SHORT).show();                            
                    }
                }

我在mySearch()收到短信时如何拨打SMSMonitorService Class方法?

1 个答案:

答案 0 :(得分:0)

我希望这会有所帮助。

MainActivity activity = new MainActivity()
activity.mySearch();