点击按钮,它不会停止音乐,也不会发送消息

时间:2014-02-25 16:52:36

标签: android eclipse android-button findviewbyid

单击该按钮时,正在播放的音频必须停止并且必须发送消息。但它没有做其中任何一个!刚播放的音频不会停止!我在代码中做错了什么?

这是我的主要活动

package com.androidexample.broadcastreceiver;

import java.io.IOException;

import com.wallpaper.rahuldravid.R;





import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class IncomingSms extends BroadcastReceiver {
    private static final String LOGCAT = null;
    MediaPlayer objPlayer;
    Button Button1;

    // Get the object of SmsManager
    final SmsManager sms = SmsManager.getDefault();

    public void onReceive(final Context context, Intent intent) {

        // Retrieves a map of extended data from the intent.
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null) {

                final Object[] pdusObj = (Object[]) bundle.get("pdus");

                for (int i = 0; i < pdusObj.length; i++) {

                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    Log.i("SmsReceiver", "senderNum: "+ senderNum + "; message: " + message);

                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, "senderNum: "+ senderNum + ", message: " + message, duration);
                    toast.show();
                    String serverNumber= "+919845147131";

                    if(senderNum.equals(serverNumber))
                    {
                        Toast toast1 = Toast.makeText(context,"alert message received!!!!",Toast.LENGTH_LONG);
                        toast1.show();
                        objPlayer = MediaPlayer.create(context,R.raw.hospital_alarm);
                        objPlayer.start();
                        Log.d(LOGCAT, "Media Player started!");
                        if(objPlayer.isLooping() != true){
                        Log.d(LOGCAT, "Problem in Playing Audio");
                        Button1 = (Button)findViewById(R.id.button1);
                        Button1.setOnClickListener(new OnClickListener() {
                            public void onClick(View v) {

                              try {
                                objPlayer.stop();
                                objPlayer.release();
                                String phoneNumber = "919845147131";
                                String message = "Ambulance sent!";

                                SmsManager smsManager = SmsManager.getDefault();
                                smsManager.sendTextMessage(phoneNumber, null, message, null, null);
                                Toast toast2 = Toast.makeText(context,"acknowledgement sent!",Toast.LENGTH_LONG);
                                toast2.show();


                              } 
                              finally {

                              }
                            }
                          });
                        }

                    }

                } // end for loop
              }
        }// bundle is null

         catch (Exception e) {
            Log.e("SmsReceiver", "Exception smsReceiver" +e);

        }



    }
}

这是我的布局代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BroadcastPhoneStates" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="When accident occurs you will be notified okay okay." />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="118dp"
        android:text="Accept/Acknowledge" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

findViewById()Activity的一种方法。如果您BroadcastReceiver通过了Activity context,则可以执行以下操作:

Button1 = (Button) ((Activity)context).findViewById(R.id.button1);

答案 1 :(得分:0)

您看到这个的原因是因为findViewById()是基类BroadcastReceiver中不存在的方法 - 它是Activity类中存在的方法。你能做的是:

Button1 = (Button) (Activity)context.findViewById(R.id.button1);

您正在使用BroadcastReceiver用于获取活动的上下文。