Android 单击按钮时,它不会停止音乐,也不会发送消息
单击按钮时,播放的音频必须停止,并且必须发送消息。但这两个都不行!音频不会停止播放!我在代码中做错了什么 这是我的主要活动Android 单击按钮时,它不会停止音乐,也不会发送消息,android,eclipse,android-button,findviewbyid,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.BroadcastRe
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);
}
}
}
package com.androidexample.broadcastreceiver;
导入java.io.IOException;
进口com.wallper.rahuldravid.R;
导入android.app.wallperManager;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.media.MediaPlayer;
导入android.media.Ringtone;
导入android.media.ringtonemager;
导入android.net.Uri;
导入android.os.Bundle;
导入android.telephony.smsmsmanager;
导入android.telephony.sms消息;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.Toast;
公共类收入SMS扩展广播接收器{
私有静态最终字符串LOGCAT=null;
媒体播放器;
按钮1;
//获取SmsManager的对象
final smsmsmanager sms=smsmsmanager.getDefault();
公共void onReceive(最终上下文、意图){
//从意图检索扩展数据的映射。
final Bundle=intent.getExtras();
试一试{
if(bundle!=null){
最终对象[]pdusObj=(对象[])bundle.get(“pdus”);
对于(int i=0;i
这是我的布局代码:
<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>
findViewById()
是一种活动的方法。如果您向您的广播接收器
传递了活动
上下文
,您可以执行以下操作:
Button1 = (Button) ((Activity)context).findViewById(R.id.button1);
之所以会看到这种情况,是因为findViewById()是基类BroadcastReceiver中不存在的方法—它是存在于Activity类中的方法。您可以做的是:
Button1 = (Button) (Activity)context.findViewById(R.id.button1);
您使用的是BroadcastReceiver用于获取活动的上下文。您的回答是正确的,只想指出这也是查看的一种方法。
:我已经消除了该错误,但单击按钮不会停止音乐或发送消息。