【问题标题】:SMS receiver didn't work短信接收器不工作
【发布时间】:2014-09-17 19:42:02
【问题描述】:

我是 android SMS 的新手,我正在尝试研究它。所以我正在尝试制作一个始终在后台运行的android应用,当我收到内容为“你好”的短信(有人通过短信向我的手机发送“你好”)时,就会播放音乐。

我在整个互联网上进行了搜索,并构建了一个可以在 AVD 上运行的项目,但是当我在手机上对其进行测试时,它却无法运行:

import info.kfsoft.android.R.raw;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.TextView;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{

    MediaPlayer ourSong;
    //static final int uniqueID=12345;
    TextView display;

    public void onReceive(Context context, Intent intent)
    {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        ourSong = MediaPlayer.create(context, R.raw.music2);


        Bundle myBundle = intent.getExtras();
        SmsMessage [] messages = null;
        String strMessage = "";

        if (myBundle != null)
        {
            Object [] pdus = (Object[]) myBundle.get("pdus");
            messages = new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++)
            {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
          //      strMessage += "SMS From the king him self: " + messages[i].getOriginatingAddress();
      //          strMessage += " : ";
               strMessage += messages[i].getMessageBody();
         //       strMessage += "\n";


            }
    //max volume
            AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                am.setStreamVolume(AudioManager.STREAM_MUSIC,am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);

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

     //       Notification n=new    Notification(android.R.drawable.)
     //       nm.notify(uniqueID,n);

            ourSong.start();
        }
    }
    public void a(){

    }

}

谢谢。

【问题讨论】:

    标签: java android eclipse sms telephony


    【解决方案1】:

    可能是您的意图优先级太低,系统安装的 SMSApp 在您的应用程序之前收到一条消息。尝试添加priority="999"

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

    另一个技巧不是在 AndroidManifest.xml 文件中注册 SMS_RECEIVED,而是在应用程序代码中以编程方式注册。这是一个示例,我实际上将多个事件注册到同一个广播接收器。

    broadcastReceiver = new EventReceiver();
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction("android.provider.Telephony.SMS_RECEIVED");
    filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY-1);
    registerReceiver(broadcastReceiver, filter);
    

    【讨论】:

    • @asdasd asd:请接受这个答案(绿色复选标记),小问题就是 SO 希望我们用户的行为方式 :)
    猜你喜欢
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多