如果我收到短信,试图发出警报!无法构建以下代码:(

时间:2011-02-19 04:30:04

标签: android sms

package com.smith.johnathan.phonefinder;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.Calendar;



import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.widget.Toast;


public class PhoneFinder extends android.content.BroadcastReceiver {

    private static final String LOG_TAG = "SMSReceiver";
    public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
    static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(PhoneFinder.ACTION)) {
            StringBuilder sb = new StringBuilder();
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                for (Object pdu : pdus) {
                    SmsMessage messages = SmsMessage
                            .createFromPdu((byte[]) pdu);
                    sb.append("Received SMS Message\nFrom: ");
                    sb.append(messages.getDisplayOriginatingAddress());
                    sb.append("\n----Message----\n");
                    sb.append(messages.getDisplayMessageBody());
                }
            }
            Log.i(PhoneFinder.LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);
            Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
        }
    }

    private void beep()
    {
        Intent intent = new Intent(PhoneFinder.class, AlarmService.class);
        startActivity(intent);

    };
}

2 个答案:

答案 0 :(得分:0)

你在onReceive()中确切地调用了这个beep函数吗?

答案 1 :(得分:0)

您创建的beep()方法永远不会被调用(因此似乎没有必要),但是如果由于某种原因您确实需要它,则需要传递一个参数以使您的Activity启动好好工作。我建议将这些代码行拖到onReceive()中,为您提供以下内容:

public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(PhoneFinder.ACTION)) {
        StringBuilder sb = new StringBuilder();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            for (Object pdu : pdus) {
                SmsMessage messages = SmsMessage
                        .createFromPdu((byte[]) pdu);
                sb.append("Received SMS Message\nFrom: ");
                sb.append(messages.getDisplayOriginatingAddress());
                sb.append("\n----Message----\n");
                sb.append(messages.getDisplayMessageBody());
            }
        }
        Log.i(PhoneFinder.LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);
        Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();

        Intent startIntent = new Intent(context, AlarmService.class);
        startActivity(startIntent);
    }
}

我重命名了您的Intent,因此它与传入的参数名称不同。您还必须确保您在应用程序中使用的每个Activity都在AndroidManifest中定义;当您尝试启动未使用<activity>标记定义的新活动时,您的应用程序将崩溃。

如果您在构建或运行时有其他错误,请明确发布输出。

我强烈建议您阅读this SDK document

干杯。