2012-11-30 20 views
0

我有一個關於在an​​droid中發送短信的問題。我有兩個班。其中一個是SendMessage.java,另一個是SendingSms.java,我想通過SendingSms.java文件發送我的消息,所以它裏面有一個sendSms類,我想在SendMessage.java文件中使用這個sendSms函數,但它可以不會的。另外,我可以使用SendingSms.java中的函數,但是當我寫入發送短信代碼時,它不起作用。有什麼問題?如何在Android中從另一個活動調用一個函數?

這裏是函數,是我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(); 
     } 

} 

順便說一句,我伸出我的SendMessage函數類從SendingSms類。

,這裏是我的logcat

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 
+0

你能粘貼一些代碼嗎?我會更容易;-) – radzio

+0

也許你可以發佈一些代碼? –

+0

我編輯了我的問題,代碼在那裏。 – mertaydin

回答

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(); 
     } 

} 
+1

好吧,我編輯了我的問題。但是,有一個問題,如果我讓sendSms函數爲靜態,我得到錯誤。 – mertaydin

+0

你正在得到哪個錯誤......? – itsrajesh4uguys

+0

謝謝,我在努力。 – mertaydin

0

您可以通過兩種方法實現:

傳中,要調用的方法的方法是存在成其他活動的活動的情況下。

2.第二種方法是使方法靜態,然後使用classname與其他活動調用它。運營商

如:

Class_Name.method()

1

所有你缺少一個許可的第一:

產生的原因:java.lang.SecurityException異常:發送短信:用戶10048沒有android.permission.SEND_SMS

其次,這種方法不應該是靜態的 - 你可以有內存泄漏。

如果SendMessage函數擴展SendSms你應該讓SendSms.sendSms方法公開,它應該工作

你不應該使用:靜態上下文的背景下;

上下文應該是正常字段。爲什麼你需要它:>?您可以使用活動上下文:>

2

首先初始化sendMessage中的類。java

sendinsms sm = new sendinsms();

sm.sendsms(your parameter);

相關問題