在Android中在后台接收短信

时间:2014-06-15 06:01:03

标签: android eclipse sms

我正在创建两个Android应用程序 - 1. Controller-App和2. Simulator-App,其中Controller-app将特定消息作为SMS发送到Simulator-app,它必须阅读此消息并执行一些动作。

我面临的问题是,一旦我的模拟器收到短信,应用程序就会被迫停止。

我希望能够在不中断我的应用程序的情况下在后台阅读传入的短信内容。

请帮忙!

这是我的主要活动的代码:

package com.exampleabc.home;

import android.os.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import android.telephony.SmsManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.widget.TextView;

public class MainAct extends Activity {

IntentFilter intentFilter;

private BroadcastReceiver intentReceiver = new BroadcastReceiver()
{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        TextView SMSes = (TextView) findViewById(R.id.textView1);
        SMSes.setText(intent.getExtras().getString("sms"));
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.act_main);
    Button bed = (Button) findViewById(R.id.bedroom);
    Button den = (Button)findViewById(R.id.den);
    Button kit = (Button) findViewById(R.id.kitchen);

    Button sms = (Button) findViewById(R.id.sms);

    intentFilter = new IntentFilter();
    intentFilter.addAction("SMS_RECEIVED SCTION");

    sms.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            sendSMS("5556","hello there!");
        }
    });



    kit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent k=new Intent(MainAct.this,Kitchen.class);
            startActivityForResult(k,0);

        }
    });

    den.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent j=new Intent(MainAct.this,Den.class);
            startActivityForResult(j,0);

        }
    });

    bed.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent i=new Intent(MainAct.this,Bedroom.class);
            startActivityForResult(i,0);

        }
    });
}

protected void onResume()
{
    registerReceiver(intentReceiver,intentFilter);
    super.onResume();
}

protected void onPause()
{
    unregisterReceiver(intentReceiver);
    super.onPause();
}

private void sendSMS(String PhnNum, String Messg)
{
    SmsManager sms= SmsManager.getDefault();
    sms.sendTextMessage(PhnNum,null,Messg,null,null);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.act_main, menu);
    return true;
}

}

这是我的SMSReceiver类的代码:

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
{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if (bundle!=null)
    {
        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();
            str+=" :";
            str+=msgs[i].getMessageBody().toString();
            str+="\n";
        }


        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("SMS_RECEIVED_ACTION");
        broadcastIntent.putExtra("sms", str);
        context.sendBroadcast(broadcastIntent);

    }
}

}

这是我的LogCat:

06-15 11:44:41.851: W/KeyCharacterMap(465): No keyboard for id 0

06-15 11:44:41.851: W/KeyCharacterMap(465): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

06-15 11:45:06.711: D/AndroidRuntime(465): Shutting down VM

06-15 11:45:06.724: W/dalvikvm(465): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

06-15 11:45:06.781: E/AndroidRuntime(465): FATAL EXCEPTION: main

06-15 11:45:06.781: E/AndroidRuntime(465): java.lang.RuntimeException: Unable to instantiate receiver com.exampleabc.home.SMSReceiver: java.lang.ClassNotFoundException: com.exampleabc.home.SMSReceiver in loader dalvik.system.PathClassLoader[/data/app/com.exampleabc.home-2.apk]

06-15 11:45:06.781: E/AndroidRuntime(465):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)

06-15 11:45:06.781: E/AndroidRuntime(465):  at android.app.ActivityThread.access$3200(ActivityThread.java:125)

06-15 11:45:06.781: E/AndroidRuntime(465):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)

06-15 11:45:06.781: E/AndroidRuntime(465):  at android.os.Handler.dispatchMessage(Handler.java:99)

06-15 11:45:06.781: E/AndroidRuntime(465):  at android.os.Looper.loop(Looper.java:123)

06-15 11:45:06.781: E/AndroidRuntime(465):  at android.app.ActivityThread.main(ActivityThread.java:4627)

06-15 11:45:06.781: E/AndroidRuntime(465):  at java.lang.reflect.Method.invokeNative(Native Method)

06-15 11:45:06.781: E/AndroidRuntime(465):  at java.lang.reflect.Method.invoke(Method.java:521)

06-15 11:45:06.781: E/AndroidRuntime(465):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

06-15 11:45:06.781: E/AndroidRuntime(465):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

06-15 11:45:06.781: E/AndroidRuntime(465):  at dalvik.system.NativeStart.main(Native Method)

06-15 11:45:06.781: E/AndroidRuntime(465): Caused by: java.lang.ClassNotFoundException: com.exampleabc.home.SMSReceiver in loader dalvik.system.PathClassLoader[/data/app/com.exampleabc.home-2.apk]

06-15 11:45:06.781: E/AndroidRuntime(465):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)

06-15 11:45:06.781: E/AndroidRuntime(465):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)

06-15 11:45:06.781: E/AndroidRuntime(465):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)

06-15 11:45:06.781: E/AndroidRuntime(465):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2780)

06-15 11:45:06.781: E/AndroidRuntime(465):  ... 10 more
06-15 11:45:10.506: I/Process(465): Sending signal. PID: 465 SIG: 9

1 个答案:

答案 0 :(得分:0)

您应该检查此项目。我写了一个接收和发送短信的例子

https://github.com/KAPLANDROID/SmsForwarderForAndroid