【问题标题】:How can i call a function in activity from another activity in android?如何从android中的另一个活动调用活动中的函数?
【发布时间】:2012-11-30 09:27:38
【问题描述】:

我有一个关于在 android 中发送短信的问题。我有两节课。其中一个是 SendMessage.java,另一个是 SendingSms.java,我想通过 SendingSms.java 文件发送我的消息,所以里面有一个 sendSms 类,我想在 SendMessage.java 文件中使用这个 sendSms 函数,但它不能不是。此外,我可以使用 SendingSms.java 中的函数,但是当我在其中编写发送短信代码时,它不起作用。有什么问题?

这是我的 SendMessage.java 中的函数

public void sendMessageButton (View view)
{
    //SendingSms send = new SendingSms();       
    if(who_detail.getText().toString().length()>0 && message_text.getText().toString().length()>0 )
    {
        sendSMS(who_detail.getText().toString(), message_text.getText().toString());

    }
    else
    {
    Toast.makeText(getApplicationContext(), R.string.sendingsms_error, Toast.LENGTH_LONG).show();
    }


}

这是我的 sendinSms.java

package com.example.stildeneme;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.widget.Toast;

public class SendingSms extends Activity {

     public void sendSMS(String telNo, String mesaj)
        {
            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) {
                    // TODO Auto-generated method stub
                    switch(getResultCode())
                    {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS Gönderildi",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));


            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(), "SMS iletildi",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case Activity.RESULT_CANCELED:
                            Toast.makeText(getBaseContext(), "SMS iletilemedi",
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }
                }

            }, new IntentFilter(DELIVERED));       

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(telNo, null, mesaj, sentPI, deliveredPI);

         Toast.makeText(getApplicationContext(), telNo.toString() + " - " + mesaj.toString(), Toast.LENGTH_LONG).show();
        }

}

顺便说一句,我从 SendingSms 类扩展了我的 SendMessage 类。

这是我的日志

11-30 09:48:09.613: E/AndroidRuntime(1007): FATAL EXCEPTION: main
11-30 09:48:09.613: E/AndroidRuntime(1007): java.lang.IllegalStateException: Could not execute method of the activity
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.view.View$1.onClick(View.java:2144)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.view.View.performClick(View.java:2485)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.view.View$PerformClick.run(View.java:9080)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.os.Handler.handleCallback(Handler.java:587)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.os.Looper.loop(Looper.java:123)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at java.lang.reflect.Method.invoke(Method.java:507)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at dalvik.system.NativeStart.main(Native Method)
11-30 09:48:09.613: E/AndroidRuntime(1007): Caused by: java.lang.reflect.InvocationTargetException
11-30 09:48:09.613: E/AndroidRuntime(1007):     at java.lang.reflect.Method.invokeNative(Native Method)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at java.lang.reflect.Method.invoke(Method.java:507)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.view.View$1.onClick(View.java:2139)
11-30 09:48:09.613: E/AndroidRuntime(1007):     ... 11 more
11-30 09:48:09.613: E/AndroidRuntime(1007): Caused by: java.lang.SecurityException: Sending SMS message: User 10048 does not have android.permission.SEND_SMS.
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.os.Parcel.readException(Parcel.java:1322)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.os.Parcel.readException(Parcel.java:1276)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at com.android.internal.telephony.ISms$Stub$Proxy.sendText(ISms.java:369)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at android.telephony.SmsManager.sendTextMessage(SmsManager.java:87)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at com.example.stildeneme.SendingSms.sendSMS(SendingSms.java:69)
11-30 09:48:09.613: E/AndroidRuntime(1007):     at com.example.stildeneme.SendMessage.sendMessageButton(SendMessage.java:76)
11-30 09:48:09.613: E/AndroidRuntime(1007):     ... 14 more

【问题讨论】:

  • 你能粘贴一些代码吗?我会更容易;-)
  • 也许你可以发布一些代码?
  • 我编辑了我的问题,代码在里面。

标签: android android-activity sendmessage


【解决方案1】:

将方法设为公共静态,您将从另一个活动中访问它。然后 将其称为 activity1.sendsms();

就是这样。如果您发布代码会为您提供更好的建议...希望这会对您有所帮助。

您好,请使用以下代码。问题是你需要使用上下文。

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.Toast;

public class SendingSms extends Activity {

    static Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        context=this;

    }

     public static void sendSMS(String telNo, String mesaj)
        {
            String SENT = "SMS_SENT";
            String DELIVERED = "SMS_DELIVERED";

            PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent(SENT), 0);

            PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED), 0);

            context.registerReceiver(new BroadcastReceiver() {

                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    // TODO Auto-generated method stub
                    switch(getResultCode())
                    {
                    case Activity.RESULT_OK:
                        Toast.makeText(context, "SMS Gönderildi",Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                         Toast.makeText(context, "Generic failure",Toast.LENGTH_SHORT).show();
                         break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                         Toast.makeText(context, "No service",Toast.LENGTH_SHORT).show();
                         break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(context, "Null PDU",Toast.LENGTH_SHORT).show();
                        break;                  
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(context, "Radio off",Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
            }, new IntentFilter(SENT));


            context. registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(context, "SMS iletildi",
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case Activity.RESULT_CANCELED:
                            Toast.makeText(context, "SMS iletilemedi",
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }
                }

            }, new IntentFilter(DELIVERED));       

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(telNo, null, mesaj, sentPI, deliveredPI);

         Toast.makeText(context, telNo.toString() + " - " + mesaj.toString(), Toast.LENGTH_LONG).show();
        }

} 

【讨论】:

  • 好的,我编辑了我的问题。但是有一个问题,如果我将 sendSms 函数设为静态,就会出错。
  • 嗨,您似乎没有添加权限。请添加它。
  • 啊,愚蠢的我,我忘记了,我正在和这个交战:) 非常感谢。 :)
【解决方案2】:

首先在你的 sendMessage.java 中初始化类

sendinsms sm=new sendinsms();

sm.sendsms(你的参数);

【讨论】:

    【解决方案3】:

    首先,您缺少一个权限:

    > Caused by: java.lang.SecurityException: Sending SMS message: User
    > 10048 does not have android.permission.SEND_SMS.
    

    其次,这个方法不应该是static - 你可能有内存泄漏。

    如果你 SendMessage 扩展了 SendSms,你应该公开 SendSms.sendSms 方法,它应该可以工作

    你不应该使用:static Context context;

    context 应该是一个普通字段。为什么你需要它:>?你可以使用activity context

    【讨论】:

      【解决方案4】:

      您可以通过两种方式进行:

      1.将存在该方法的活动的上下文传递给您要调用该方法的其他活动。

      2. 第二种方法是将方法设为静态,然后使用带有 .运营商

      例如:

      Class_Name.method()

      【讨论】:

        猜你喜欢
        • 2019-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多