【问题标题】:How to Combine my two projects 1.)SmsReceiver projec and 2.)Caller project如何结合我的两个项目 1.)SmsReceiver 项目和 2.)Caller 项目
【发布时间】:2018-06-19 00:42:39
【问题描述】:

我不想在我的 android 中构建 SMS 触发的应用程序。我知道市场上有很多应用程序可以做到这一点,但我只想自己尝试一下。

我的 SMS 接收器项目和我的 Caller 项目一样有效。我想将它们组合为 1 个应用程序,但我不知道。有人可以帮帮我吗?

这是我从某处获得并测试工作的代码 sn-p..

调用者.java

package pi.redphone;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;



public class Caller extends Activity 
{
    private Context context;

    /** Called when the activity is first created. */
    @Override
       public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String num = "2217238";
        call(num);
    } 

 public void call(String number) 
{
    try 
    {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:"+number));
        startActivity(callIntent);
    } 
    catch (ActivityNotFoundException activityException) 
    {
        Log.e("dialing-example", "Call failed", activityException);
    }
}

}

上面代码中的电话号码是硬编码的,安装后会拨打这个号码2217238

SmsReceiver.java

package pi.redphone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver
{


    public void onReceive(Context context, Intent intent) 
    {
        Bundle bundle = intent.getExtras();
        //SmsMessage msg = null;
        String sender = null;
        String msgBody = null;

        if(bundle != null)
        {
            Object[] sms = (Object[]) bundle.get("pdus");

            for(int i=0; i<sms.length; i++)
            {
                SmsMessage msg = SmsMessage.createFromPdu((byte[]) sms[i]);
                sender = msg.getOriginatingAddress(); //store sender's mobile #
                msgBody = msg.getMessageBody(); //msg content

            }
            Toast.makeText(context, sender, Toast.LENGTH_LONG ).show();


        }

    }

}

安装后app会报这个错误

呼叫转移应用程序意外停止工作。请 再试一次。。

实际上我想做的是当有人向我的 android 发送短信时,我的 SmsReceiver 将提取发件人手机号码并回拨.. 发件人的号码存储在 sender 变量中..

我想做这样的事情

call(sender);

但这并不容易..我可以合并源代码而没有错误,但安装后我无法让它工作......

如果我能让它工作的话,这将是我的第一个 Android 应用程序.. ;)


这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pi.redphone"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_SMS">

    </uses-permission>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".callForward"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的 LogCAt:

01-15 03:11:50.265: I/jdwp(289): Ignoring second debugger -- accepting and dropping
01-15 03:12:30.756: W/KeyCharacterMap(289): No keyboard for id 0
01-15 03:12:30.756: W/KeyCharacterMap(289): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-15 03:13:05.095: D/dalvikvm(289): GC_EXPLICIT freed 1347 objects / 90832 bytes in 74ms
01-15 03:18:25.995: D/AndroidRuntime(357): Shutting down VM
01-15 03:18:26.045: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 03:18:26.115: E/AndroidRuntime(357): FATAL EXCEPTION: main
01-15 03:18:26.115: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{pi.redphone/pi.redphone.callForward}: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.os.Looper.loop(Looper.java:123)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 03:18:26.115: E/AndroidRuntime(357):  at java.lang.reflect.Method.invokeNative(Native Method)
01-15 03:18:26.115: E/AndroidRuntime(357):  at java.lang.reflect.Method.invoke(Method.java:521)
01-15 03:18:26.115: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 03:18:26.115: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 03:18:26.115: E/AndroidRuntime(357):  at dalvik.system.NativeStart.main(Native Method)
01-15 03:18:26.115: E/AndroidRuntime(357): Caused by: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-15 03:18:26.115: E/AndroidRuntime(357):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
01-15 03:18:26.115: E/AndroidRuntime(357):  ... 11 more
01-15 03:18:35.085: I/Process(357): Sending signal. PID: 357 SIG: 9
01-15 03:31:01.475: D/AndroidRuntime(449): Shutting down VM
01-15 03:31:01.505: W/dalvikvm(449): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-15 03:31:01.555: E/AndroidRuntime(449): FATAL EXCEPTION: main
01-15 03:31:01.555: E/AndroidRuntime(449): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{pi.redphone/pi.redphone.callForward}: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.os.Looper.loop(Looper.java:123)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-15 03:31:01.555: E/AndroidRuntime(449):  at java.lang.reflect.Method.invokeNative(Native Method)
01-15 03:31:01.555: E/AndroidRuntime(449):  at java.lang.reflect.Method.invoke(Method.java:521)
01-15 03:31:01.555: E/AndroidRuntime(449):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-15 03:31:01.555: E/AndroidRuntime(449):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-15 03:31:01.555: E/AndroidRuntime(449):  at dalvik.system.NativeStart.main(Native Method)
01-15 03:31:01.555: E/AndroidRuntime(449): Caused by: java.lang.ClassCastException: pi.redphone.callForward
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-15 03:31:01.555: E/AndroidRuntime(449):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
01-15 03:31:01.555: E/AndroidRuntime(449):  ... 11 more
01-15 03:31:15.255: I/Process(449): Sending signal. PID: 449 SIG: 9

应用程序仍然没有运行。这是我的全部源代码。

package pi.redphone;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class callForward extends BroadcastReceiver
 {
    /** Called when the activity is first created. */
    public void onReceive(Context context, Intent intent) 
    {
        Bundle bundle = intent.getExtras();
        //SmsMessage msg = null;
        String sender = null;
        String msgBody = null;

        if(bundle != null)
        {
            Object[] sms = (Object[]) bundle.get("pdus");

            for(int i=0; i<sms.length; i++)
            {
                SmsMessage msg = SmsMessage.createFromPdu((byte[]) sms[i]);
                sender = msg.getOriginatingAddress(); //store sender's mobile #
                msgBody = msg.getMessageBody(); //msg content

            }
            Toast.makeText(context, sender, Toast.LENGTH_LONG ).show();


        }

    }

}

我感觉代码是正确的,但我不知道如何解决这个问题..


我不知道如何遵循您的建议,因为我总是遇到错误。而是帮我检查我是否做得对..

这是我最新的代码。通话正常,但我的短信接收器没有。

我不知道怎么称呼我的

public class SmsReceiver extends BroadcastReceiver 

在活动中,以便它监听传入的短信。

Call4wardActivity.Java

package pi.redphone;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class Call4wardActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            call();

        }



    public void call() 
    {
        try {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:123456789"));
            startActivity(callIntent);
        } catch (ActivityNotFoundException activityException) {
             Log.e("helloandroid dialing example", "Call failed", null);
        }
    }

    public class SmsReceiver extends BroadcastReceiver 
    {


        @Override
        public void onReceive(Context context, Intent intent) 
        {
            // TODO Auto-generated method stub
            Bundle bundle = intent.getExtras();        
            SmsMessage[] msgs = null;
           // String body = null;
            String sender = null;


            if (bundle != null)
            {
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];   

                for (int i=0; i<msgs.length; i++)
                {
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);  
                    sender = msgs[i].getOriginatingAddress();

                }
                //---display the new SMS message---
                Toast.makeText(context, sender, Toast.LENGTH_LONG).show();

            }                       
        }

        }


    }

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pi.redphone"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Call4wardActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

    <uses-permission android:name="android.permission.CALL_PHONE"/>

</manifest>

【问题讨论】:

    标签: android phone-call forwarding


    【解决方案1】:

    您必须发布您的 logcat 错误以获取有关崩溃的帮助。不过打个电话也不是什么难事。我最初的猜测是您的 android 清单中缺少权限。它是否拥有 SMS 和 Dialer 应用程序的所有权限?

    确保发件人只存储电话号码。我相信是的。

    您确实不需要包含第一个应用程序。您需要的唯一代码是

    try  
    {  
        Intent callIntent = new Intent(Intent.ACTION_CALL);  
        callIntent.setData(Uri.parse("tel:"+number));  
        startActivity(callIntent);  
    }   
    catch (ActivityNotFoundException activityException)   
    {  
        Log.e("dialing-example", "Call failed", activityException);  
    }  
    

    用发件人替换号码,你应该都设置好了。

    如果您想保留这两个活动,您可以执行以下操作。 意图 callIntent = new Intent(this, Caller.class);
    callIntent.putExtra("phoneNumber", sender);
    开始活动(调用意图);

    在通话活动中,你会

    Bundle extras = getIntent().getExtras();  
    string sender = extras.getInt("phoneNumber");  
    

    然后您可以提示用户并询问您是否希望应用程序拨打该号码。或者您可以立即调用它。如果您只是在没有确认的情况下立即调用 #,请使用我的帖子中提到的第一种解决方案。

    【讨论】:

    • 我让短信接收器正常工作,但我应该如何调用 startActivity(callIntent); ?
    • 我已添加编辑。我不知道您是想自动呼叫还是启动具有呼叫按钮的应用程序。但我提供了代码来启动呼叫者活动并发送电话号码
    • 我已经在我目前的工作上面添加了编辑...关于这个的问题。 callIntent = new Intent(this, Caller.class); callIntent.putExtra("phoneNumber", sender);您告诉我不要使用第一个文件,但为什么要使用另一个文件中的 Caller 类?对不起,我无法得到它..
    • 您的解决方案正是我试图帮助您实现的目标。我的代码模棱两可,因为您似乎坚持要保留 startActivity(callIntent)。你永远不需要我向你解释的调用活动。
    • 谢谢。现在我有另一个关于电话特殊字符的问题。请在这里查看我的其他帖子stackoverflow.com/questions/9048833/…
    【解决方案2】:

    我终于解决了这个.. :)

    这是基本代码..它将调用短信发送者..

    SmsReceiver.java

    package romel.pi.redphone;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.util.Log;
    
    public class SmsReceiver extends BroadcastReceiver 
    {
    
        private Context context;
    
        private static final String frwdcode = "**21*";
        String phoneNumber = "";
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            System.out.println("Starting Receiver");
    
            //---get the SMS message passed in---
            Bundle bundle = intent.getExtras();        
            SmsMessage[] msgs = null;
            String str = "";            
            if (bundle != null)
            {
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];            
                for (int i=0; i<msgs.length; i++){
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                    //str += "SMS from " + msgs[i].getOriginatingAddress();  
                    phoneNumber = msgs[i].getOriginatingAddress();
                    str += "\n\nMessage:\n\n";
                    str += msgs[i].getMessageBody().toString();
                    str += "\n";        
                }
                try 
                {   
    
                    Log.v("SmsReceiver", "entering TRY");
                    this.context = context;
                    MessageBodyParserTrigger();
    
    
    
    
    
    
    
                } 
                catch (Exception e) 
                {   
                    Log.v("SendMail", e.getMessage(), e);   
                } 
            } 
    
    
        }
    
        void MessageBodyParserTrigger()
        {
    
            Object localObject1 = new Intent("android.intent.action.CALL");
            Object localObject2 = Uri.encode("#");
            ((Intent)localObject1).setData(Uri.parse("tel:"+ "**21*" + phoneNumber + localObject2));
            ((Intent)localObject1).addFlags(268435456);
            this.context.startActivity((Intent)localObject1);
            this.context.stopService(null);
    
        }
    
    
    
    }
    

    清单

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="romel.pi.redphone"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk android:minSdkVersion="8" />
    
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
        <uses-permission android:name="android.permission.READ_SMS" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.CALL_PHONE" />
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
    
    
    
    
            <receiver android:name=".SmsReceiver"> 
                <intent-filter> 
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
                </intent-filter> 
            </receiver>
    
    
    
    
    
        </application>
    
    
    
    
    </manifest>
    

    希望对其他人有所帮助..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-24
      • 2015-02-06
      • 1970-01-01
      • 2023-03-18
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      相关资源
      最近更新 更多