Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.support.v4.content.LocalBroadcastManager;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- //MainActivity.java
- public class MainActivity extends AppCompatActivity implements IJRReciever{
- private Button mButton,mNextActivity;
- private TextView mtext;
- private MyReciever mReciever;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mtext = (TextView) findViewById(R.id.info_text);
- mButton = (Button) findViewById(R.id.send_btn);
- mNextActivity = (Button) findViewById(R.id.next_activity);
- mButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if(mReciever == null){
- mReciever = new MyReciever();
- mReciever.setListener(MainActivity.this);
- /* IntentFilter filter = new IntentFilter();
- filter.addAction("com.navi.test");
- filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
- Log.d("SmsReceiver", this.getClass().getSimpleName() + " : " + "Register");
- registerReceiver(mReciever,filter);*/
- Intent intent = new Intent(MainActivity.this,SecondActivity.class);
- startActivity(intent);
- }
- }
- });
- mNextActivity.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(MainActivity.this,SecondActivity.class);
- startActivity(intent);
- }
- });
- }
- @Override
- public void getMessageFromReciever(String message) {
- Toast.makeText(this,message,Toast.LENGTH_LONG).show();
- unregisterSmsReceiver();
- }
- private void unregisterSmsReceiver() {
- try {
- if (mReciever != null) {
- unregisterReceiver(mReciever);
- mReciever.setListener(null);
- mReciever = null;
- Log.d("SmsReceiver", this.getClass().getSimpleName() + " : " + "unregister");
- }
- } catch (Exception e) {
- }
- }
- }
- //SecondActivity .java
- public class SecondActivity extends AppCompatActivity{
- private Button prevButton;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.second_activity);
- prevButton = (Button) findViewById(R.id.prev_activity);
- prevButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intents = new Intent(SecondActivity.this,ThirdActivity.class);
- startActivity(intents);
- }
- });
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- Intent intent = new Intent();
- intent.setAction("com.navi.test");
- intent.putExtra("INFORMATION","SENT FROM SECOND ACTIVITY");
- sendBroadcast(intent);
- }
- }
- //ThirdActivity .java
- public class ThirdActivity extends AppCompatActivity implements IJRReciever{
- private MyReciever mReciever;
- private Button mBotton;
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.third_activity);
- mBotton = (Button) findViewById(R.id.fourth_act_btn);
- mBotton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if(mReciever == null){
- mReciever = new MyReciever();
- mReciever.setListener(ThirdActivity.this);
- /* IntentFilter filter = new IntentFilter();
- filter.addAction("com.navi.test");
- filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
- Log.d("SmsReceiver", this.getClass().getSimpleName() + " : " + "Register");
- registerReceiver(mReciever,filter);*/
- Intent inte =new Intent(ThirdActivity.this,FourthActivity.class);
- startActivity(inte);
- }
- }
- });
- }
- @Override
- public void getMessageFromReciever(String message) {
- Toast.makeText(this,message,Toast.LENGTH_LONG).show();
- unregisterSmsReceiver();
- }
- private void unregisterSmsReceiver() {
- try {
- if (mReciever != null) {
- unregisterReceiver(mReciever);
- mReciever.setListener(null);
- mReciever = null;
- Log.d("SmsReceiver", this.getClass().getSimpleName() + " : " + "unregister");
- }
- } catch (Exception e) {
- }
- }
- }
- //FourthActivity .java
- public class FourthActivity extends AppCompatActivity{
- @Override
- protected void onCreate(@Nullable Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.fourth_activity);
- }
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- Intent intent = new Intent();
- intent.setAction("com.navi.test");
- intent.putExtra("INFORMATION","SENT FROM FOURTH ACTIVITY");
- sendBroadcast(intent);
- }
- }
- //MyReciever.java
- public class MyReciever extends BroadcastReceiver{
- private String message;
- private IJRReciever mListener;
- public void setListener(IJRReciever listener){
- this.mListener = listener;
- }
- @Override
- public void onReceive(Context context, Intent intent) {
- message = intent.getStringExtra("INFORMATION");
- Toast.makeText(context,"Message Recieved in MyReciever",Toast.LENGTH_LONG).show();
- //listener works when i register reciever programatically, else it will be null, now i have registered in Menifest so it wont work, control will skip this step
- if(mListener != null){
- mListener.getMessageFromReciever(message);
- }
- }
- }
- //AndroidMenifest.xml File
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.naveenbm.broadcastreciever">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".SecondActivity">
- </activity>
- <activity android:name=".ThirdActivity">
- </activity>
- <activity android:name=".FourthActivity">
- </activity>
- <receiver
- android:name=".MyReciever"
- android:enabled="true"
- android:exported="true" >
- <intent-filter>
- <action android:name="com.navi.test" />
- </intent-filter>
- </receiver>
- </application>
- </manifest>
- //activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <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="com.example.naveenbm.broadcastreciever.MainActivity"
- android:gravity="center_horizontal">
- <TextView
- android:id="@+id/info_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Send Custom Braodcast"
- android:textColor="@color/black"
- />
- <Button
- android:id="@+id/send_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Register BroadCast and Go to Next Activity"
- android:textSize="16sp"
- android:layout_below="@id/info_text"
- android:layout_marginTop="20dp"/>
- <Button
- android:id="@+id/next_activity"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Next Activity"
- android:textColor="@color/black"
- android:textSize="16sp"
- android:layout_marginTop="20dp"
- android:layout_below="@+id/send_btn"/>
- </RelativeLayout>
- //second_activity.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Second Activity"
- android:layout_gravity="center_horizontal"
- android:textSize="16sp"
- android:textColor="@color/black"/>
- <TextView
- android:id="@+id/text_val_sec"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="16sp"
- android:layout_margin="20dp"
- android:layout_gravity="center_horizontal"
- android:textColor="@color/black"/>
- <Button
- android:id="@+id/prev_activity"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Go to Third Activity"
- android:textColor="@color/black"
- android:textSize="16sp"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="20dp"/>
- </LinearLayout>
- //third_activity.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Third Activity"
- android:textSize="16sp"
- android:textColor="@color/black"/>
- <Button
- android:id="@+id/fourth_act_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Go to Fourth Activity"
- android:textSize="16sp"
- android:textColor="@color/black"
- android:layout_marginTop="20dp"/>
- </LinearLayout>
- //fourth_activity.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Fourth Activity"
- android:textSize="16sp"
- android:layout_gravity="center_horizontal"
- android:textColor="@color/black"/>
- </LinearLayout>
Add Comment
Please, Sign In to add comment