开发者

Trying to set off a alarm if I get a SMS message!! Cant build the following code :(

开发者 https://www.devze.com 2023-02-11 20:18 出处:网络
package com.smith.johnathan.phonefinder; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent;
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);

    };
}


Where exactly in onReceive() have you called this beep function?


The beep() method that you have created is never getting called (so it seems to have no need), however if for some reason you do need it you will need to pass it a parameter to make your Activity start work properly. I would recommend pulling those lines of code into onReceive(), giving you the following:

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

I renamed your Intent so it doesn't have the same name as the parameter being passed in. You also must make sure that every Activity you use in your application is defined in the AndroidManifest; your application will crash when you try to start a new Activity you didn't define with an <activity> tag.

If you have further errors at build or runtime, please post the output explicitly.

I highly recommend reading this SDK document as well.

Cheers.

0

精彩评论

暂无评论...
验证码 换一张
取 消