在按钮的点击它不会停止音乐也不会发送消息 [英] On click of the button it does not stop the music nor does it send a message
本文介绍了在按钮的点击它不会停止音乐也不会发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当按钮被点击正在播放已停止和一个消息具有要发送的音频。但它没有做任何他们!音频只是起着不会停止!我做了什么错在code ??
下面是我的主要活动
包com.androidexample.broadcastreceiver;进口java.io.IOException异常;进口com.wallpaper.rahuldravid.R;进口android.app.WallpaperManager;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.media.MediaPlayer;
进口android.media.Ringtone;
进口android.media.RingtoneManager;
进口android.net.Uri;
进口android.os.Bundle;
进口android.telephony.SmsManager;
进口android.telephony.SmsMessage;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.Toast;
公共类IncomingSms扩展广播接收器{
私有静态最后弦乐的logcat = NULL;
MediaPlayer的objPlayer;
按钮Button1的; //获取SmsManager对象
最后SmsManager短信= SmsManager.getDefault(); 公共无效的onReceive(最终上下文的背景下,意图意图){ //获取地图从意图扩展数据。
最后的捆绑包= intent.getExtras(); 尝试{ 如果(捆绑!= NULL){ 最终目标[] = pdusObj(对象[])bundle.get(的PDU); 的for(int i = 0; I< pdusObj.length;我++){ SmsMessage currentMessage = SmsMessage.createFromPdu((字节[])pdusObj [I]);
串phoneNumber的= currentMessage.getDisplayOriginatingAddress(); 字符串senderNum = phoneNumber的;
字符串消息= currentMessage.getDisplayMessageBody(); Log.i(SmsReceiver,senderNum:+ senderNum +的消息:+消息); INT持续时间= Toast.LENGTH_SHORT;
吐司面包= Toast.makeText(背景下,senderNum:+ senderNum +,留言:+消息,持续时间);
toast.show();
字符串serverNumber =919845147131; 如果(senderNum.equals(serverNumber))
{
吐司toast1 = Toast.makeText(背景下,警报消息收到!!!!,Toast.LENGTH_LONG);
toast1.show();
objPlayer = MediaPlayer.create(背景下,R.raw.hospital_alarm);
objPlayer.start();
Log.d(logcat中,媒体播放器开始了!);
如果(objPlayer.isLooping()!= TRUE){
Log.d(logcat中,问题在播放音频);
Button1的=(按钮)findViewById(R.id.button1);
Button1.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){ 尝试{
objPlayer.stop();
objPlayer.release();
字符串phoneNumber的=919845147131;
字符串消息=救护车送! SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber的,空的消息,NULL,NULL);
吐司烤面包= Toast.makeText(背景下,确认送!,Toast.LENGTH_LONG);
toast2.show();
}
最后{ }
}
});
} } } //结束for循环
}
} //包是空 赶上(例外五){
Log.e(SmsReceiver,异常smsReceiver+ E); } }
}
下面是我的布局code:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:BroadcastPhoneStates上下文=> <的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:文字=当事故发生时将通知您还好还好。 /> <按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / textView1
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =118dp
机器人:文字=接受/确认/>< / RelativeLayout的>
解决方案
findViewById()
是活动的方法
。如果你的广播接收器
您通过了活动
背景
即可这样做:
Button1的=(按钮)((活动)上下文).findViewById(R.id.button1);
When the button is clicked the audio that is playing has to stop and a message has to be sent. But its not doing either of them! The audio just plays does not stop! What did I do wrong in the code??
Here is my main activity
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);
}
}
}
Here is my layout code:
<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()
is a method of Activity
. If to your BroadcastReceiver
you passed the Activity
context
you can do:
Button1 = (Button) ((Activity)context).findViewById(R.id.button1);
这篇关于在按钮的点击它不会停止音乐也不会发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文