Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 短信上的广播接收器似乎没有';t火_Android_Sms_Broadcastreceiver - Fatal编程技术网

Android 短信上的广播接收器似乎没有';t火

Android 短信上的广播接收器似乎没有';t火,android,sms,broadcastreceiver,Android,Sms,Broadcastreceiver,这本书()中的“根据收到的短信自动发送短信”的诀窍对我不起作用 在模拟器上工作正常,但在真实设备上没有。为什么? 我使用了HTC Legend和android 2.3.7(cyanogenmod)。 谢谢你的帮助 在下面几行中,我将提供有关我的申请的更多细节 1) ResponderService:负责侦听传入消息的服务会根据响应进行响应 package tn.a2soft.a2soft_crm; import java.util.ArrayList; import android.app.

这本书()中的“根据收到的短信自动发送短信”的诀窍对我不起作用

在模拟器上工作正常,但在真实设备上没有。为什么? 我使用了HTC Legend和android 2.3.7(cyanogenmod)。 谢谢你的帮助

在下面几行中,我将提供有关我的申请的更多细节

1) ResponderService:负责侦听传入消息的服务会根据响应进行响应

package tn.a2soft.a2soft_crm;

import java.util.ArrayList;

import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class ResponderService extends Service {

    // The Action fired by the Android-System when a SMS was received.
    private static final String RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
    private static final String SENT_ACTION = "SENT_SMS";
    private static final String DELIVERED_ACTION = "DELIVERED_SMS";
    String requester;
    String reply = "";


    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        IntentFilter filter = new IntentFilter(RECEIVED_ACTION);
        filter.setPriority(500);
        registerReceiver(SMSreceiver, filter);

        registerReceiver(sentReceiver, new IntentFilter(SENT_ACTION));

        registerReceiver(deliverReceiver, new IntentFilter(DELIVERED_ACTION));

        //registerReceiver(sender, new IntentFilter(SENT_ACTION));
    }

//  private BroadcastReceiver sender = new BroadcastReceiver() {
//
//      @Override
//      public void onReceive(Context c, Intent i) {
//          if (i.getAction().equals(SENT_ACTION)) {
//              if (getResultCode() != Activity.RESULT_OK) {
//                  String reciptent = i.getStringExtra("recipient");
//                  requestReceived(reciptent);
//              }
//          }
//      }
//  };
    private BroadcastReceiver sentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context c, Intent in) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                // sent SMS message successfully;
                smsSent();
                break;
            default:
                // sent SMS message failed
                smsFailed();
                break;
            }
        }
    };

    private BroadcastReceiver SMSreceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context c, Intent in) {

            // TODO Auto-generated method stub
            Log.v("ResponderService", "On Receive");
            Toast.makeText(c, "Revceive SMS",Toast.LENGTH_LONG).show();
            if (in.getAction().equals(RECEIVED_ACTION)) {
                Log.v("ResponderService", "On SMS RECEIVE");
                Bundle bundle = in.getExtras();
                if (bundle != null) {

                    Object[] pdus = (Object[]) bundle.get("pdus");
                    SmsMessage[] messages = new SmsMessage[pdus.length];

                    for (int i = 0; i < pdus.length; i++) {
                        Log.v("ResponderService", "FOUNDMESSAGE");
                        messages[i] = SmsMessage
                                .createFromPdu((byte[]) pdus[i]);
                    }

                    for (SmsMessage message : messages) {
                        requestReceived(message.getOriginatingAddress());
                    }

                    respond();

                }

            }
        }
    };


    public void smsSent() {
        Toast.makeText(this, "SMS sent", Toast.LENGTH_SHORT).show();
    }

    public void smsFailed() {
        Toast.makeText(this, "SMS sent failed", Toast.LENGTH_SHORT).show();
    }

    public void smsDelivered() {
        Toast.makeText(this, "SMS delivered", Toast.LENGTH_SHORT).show();
    }

    public void requestReceived(String f) {
        Log.v("ResponderService", "In requestReceived");
        requester = f;
    }

    BroadcastReceiver deliverReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context c, Intent in) {
            // SMS delivered actions
            smsDelivered();
        }
    };



    public void respond() {
        Log.v("ResponderService", "Responing to " + requester);

        reply = "Thank you for your message. I am busy now. "
                + "I will call you later";
        SmsManager sms = SmsManager.getDefault();

        Intent sentIn = new Intent(SENT_ACTION);
//      Intent sentIn = new Intent("android.telephony.SmsManager.STATUS_ON_ICC_SENT");
        PendingIntent sentPIn = PendingIntent.getBroadcast(this, 0, sentIn, 0);

        Intent deliverIn = new Intent(DELIVERED_ACTION);
        PendingIntent deliverPIn = PendingIntent.getBroadcast(this, 0,
                deliverIn, 0);

        ArrayList<String> Msgs = sms.divideMessage(reply);
        ArrayList<PendingIntent> sentIns = new ArrayList<PendingIntent>();
        ArrayList<PendingIntent> deliverIns = new ArrayList<PendingIntent>();
        for (int i = 0; i < Msgs.size(); i++) {
            sentIns.add(sentPIn);
            deliverIns.add(deliverPIn);
        }

        sms.sendMultipartTextMessage(requester, null, Msgs, sentIns, deliverIns);
        // sms.sendTextMessage(requester, null, reply, sentPIn, deliverPIn);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(SMSreceiver);
        unregisterReceiver(deliverReceiver);
        unregisterReceiver(sentReceiver);
//      unregisterReceiver(sender);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}
3) 清单文件:

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >
        <activity
            android:name="tn.a2soft.a2soft_crm.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="tn.a2soft.a2soft_crm.SMSResponder"
            android:label="@string/app_name" >
        </activity>

<!--         <receiver
            android:name=".SMSReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.BROADCAST_SMS" >
            <intent-filter android:priority="999" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver> -->

        <service
            android:name=".ResponderService"
            android:enabled="true" >
        </service>
    </application>

</manifest>

正如我在前面所说的,这个示例可以在emulator上工作,但不能在真实设备上工作。
谢谢你的帮助,我真的很头疼。

该部分需要在清单文件中取消注释。我还建议将其简化为:

<receiver android:name=".SMSReceiver" android:enabled="true">
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>


stackoverflow论坛是程序员的首选,我找到了解决方案,并对其进行了测试,在这个论坛中,它对我来说效果很好(将broadcastreceiver的优先级设置为Integer.MAX_值)。感谢您的帮助,特别是那些试图帮助我的“PVS”。

您可以按照地下知识库的建议,将优先级增加到MAX_INT的值,但这并没有多大作用——现在似乎每个人都在这样做!如果你想更好地控制其他应用,我建议你也实现一个监听器

// Register to detect incoming SMS
incomingSmsHandler = new Handler();
contentResolver.registerContentObserver(smsUri, true, new ContentObserver(incomingSmsHandler)
{
// Etc
}

我想你应该多告诉我们一点。Telephony.SMS_收到的操作是否已发送到您的广播接收器?您是否在清单中声明了它?不,我认为广播接收器没有接收到action Telephony.SMS_,这就是应用程序不能按预期工作的原因。我发现了问题,我有一个名为“Contacts+”的应用程序,用于联系人管理(从Play Store下载),它“吃”卸载时收到的消息我的应用程序工作正常。我如何让我的应用程序与Contacts+一起工作,因为我是Android世界的新手。请准确地告诉我(根据我的示例)如何实现此处理程序。
// Register to detect incoming SMS
incomingSmsHandler = new Handler();
contentResolver.registerContentObserver(smsUri, true, new ContentObserver(incomingSmsHandler)
{
// Etc
}