English 中文(简体)
如何规划我的两个项目 1.SmsReceiver projec and 2.)Caller项目
原标题:How to Combine my two projects 1.)SmsReceiver projec and 2.)Caller project

I wan t to Building SMSstart app in my android. 我知道市场上有很多事情要做,但我只想自己尝试。

我的SMS接收器项目以及我的助手项目正在运行。 我想将它们作为1份申请合并,但我不抱任何想法。 谁能给我一手?

这里是我从某个地方经过测试的先令。

Caller.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);
    }
}

}

上述代码中的电话号码是硬编码的,在安装后将打电话到这个号码<><><>2217>238。

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


        }

    }

}

安装后,该机将给出这一错误。

The application Callforwarding has stoped working unexpectedly. Pls try again..

实际上,我想做的是,当有人向我的和roid我的SmsReceiver寄出小ms时,他们就会抽出放的手机号码,并将打回来。 变量。

我希望这样做。

call(sender);

但 这并不容易。 我可以把源代码与没有错误结合起来,但我无法在安装后工作。

This would be my first Android application if I can get this to work.. ;)


这是我明确的档案:

<?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>

这是我的后勤中心:

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

The application still don t run. here is my whole source code.

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 

在这项活动中,它倾听了新陈词。

<>Call4wardActative.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();

            }                       
        }

        }


    }

http://www.ohchr.org。

<?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>
最佳回答

您不得不在坠毁的帮助中留下错误。 但打电话太困难。 我最初的猜测是,你没有获得你和roid的许可。 它是否得到了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);  
}  

用发件人替换号码,所有数字都应确定。

If you want to keep both Activities you can do the following. Intent callIntent = new Intent(this, Caller.class );
callIntent.putExtra("phoneNumber", sender);
startActivity(callIntent);

在电话活动中,你将

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

然后,你可以让用户参与进来,并询问谁想要打电话。 或者说,你可以把它称作是正确的。 如果你在无确认的情况下重新称号,则在我的职位中使用上述第1个解决办法。

问题回答

我最后解决了这一问题。

这里是基本法典,称为SMS投稿人。

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 += "

Message:

";
                str += msgs[i].getMessageBody().toString();
                str += "
";        
            }
            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);

    }



}

<<>Manifest

<?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>

hope it can help to others..





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签