I am trying to make application, that can extends "BroadcastReceiver", and on the xml GUI there is a button, I am trying to open new Activity(GUI) when I press on it. I know how to do it when I extends Activity, but not when I extends BroadcastReceiver. this is my java code:
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(){
}
}
and this is my xml RelativeLayout that works with the previous java 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"
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>
NOW I want to open new intent when I press the button (WITH @+ID/CMD):
This is the Jave code of the new intent:
public class AboutUs extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
A simple Answer is, You can start an Activity from Receiver like this:
Intent i = new Intent(context.getApplicationContext(), AboutUs.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
This is how You are starting an Activity from Receiver. But You have to send broadcast from another activity first to "wake up" Your BroadcastReceiver.
On button click
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.