我正在尝试创建应用程序,可以扩展“BroadcastReceiver”,并且在xml GUI上有一个按钮,当我按下它时,我正在尝试打开新的Activity(GUI)。我知道在扩展Activity时如何做到这一点,但是当我扩展BroadcastReceiver时却不知道。这是我的java代码:
import info.kfsoft.android.R.raw;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.TextView;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver
{
MediaPlayer ourSong;
TextView display;
public void onReceive(Context context, Intent intent)
{
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
ourSong = MediaPlayer.create(context, R.raw.music2);
Bundle myBundle = intent.getExtras();
SmsMessage [] messages = null;
String strMessage = "";
if (myBundle != null)
{
Object [] pdus = (Object[]) myBundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
strMessage += messages[i].getMessageBody();
}
Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC,am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),0);
Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
ourSong.start();
}
}
public void a(){
}
}
这是我的xml RelativeLayout,可以使用以前的java代码:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="info.kfsoft.android.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/cmd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/msg"
android:layout_centerHorizontal="true"
android:layout_marginTop="98dp"
android:text="Button" />
</RelativeLayout>
现在我想在按下按钮时打开新意图(WITH @ + ID / CMD):
这是新意图的Jave代码:
public class AboutUs extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
答案 0 :(得分:2)
一个简单的答案是,你可以像这样从Receiver开始一个Activity:
Intent i = new Intent(context.getApplicationContext(), AboutUs.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
这是您从Receiver开始活动的方式。但是你必须先从另一个活动发送广播来“唤醒”你的BroadcastReceiver。
答案 1 :(得分:0)
点击按钮
OnClickListener listnr=new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context.getApplicationContext(), NewActivity.class);
context.startActivity(i);
}
};
Button btn =(Button) findViewById(R.id.btn);
btn.setOnClickListener(listnr);