Guest User

Untitled

a guest
Jun 2nd, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.88 KB | None | 0 0
  1. import android.content.BroadcastReceiver;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.content.IntentFilter;
  5. import android.support.v4.content.LocalBroadcastManager;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.support.v7.widget.Toolbar;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. //MainActivity.java
  16. public class MainActivity extends AppCompatActivity implements IJRReciever{
  17. private Button mButton,mNextActivity;
  18. private TextView mtext;
  19. private MyReciever mReciever;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. mtext = (TextView) findViewById(R.id.info_text);
  26.  
  27. mButton = (Button) findViewById(R.id.send_btn);
  28. mNextActivity = (Button) findViewById(R.id.next_activity);
  29. mButton.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32.  
  33. if(mReciever == null){
  34. mReciever = new MyReciever();
  35. mReciever.setListener(MainActivity.this);
  36. /* IntentFilter filter = new IntentFilter();
  37. filter.addAction("com.navi.test");
  38. filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
  39. Log.d("SmsReceiver", this.getClass().getSimpleName() + " : " + "Register");
  40. registerReceiver(mReciever,filter);*/
  41.  
  42. Intent intent = new Intent(MainActivity.this,SecondActivity.class);
  43. startActivity(intent);
  44. }
  45. }
  46. });
  47.  
  48. mNextActivity.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View v) {
  51. Intent intent = new Intent(MainActivity.this,SecondActivity.class);
  52. startActivity(intent);
  53. }
  54. });
  55. }
  56.  
  57. @Override
  58. public void getMessageFromReciever(String message) {
  59. Toast.makeText(this,message,Toast.LENGTH_LONG).show();
  60. unregisterSmsReceiver();
  61. }
  62.  
  63.  
  64. private void unregisterSmsReceiver() {
  65. try {
  66. if (mReciever != null) {
  67. unregisterReceiver(mReciever);
  68. mReciever.setListener(null);
  69. mReciever = null;
  70. Log.d("SmsReceiver", this.getClass().getSimpleName() + " : " + "unregister");
  71. }
  72. } catch (Exception e) {
  73.  
  74. }
  75. }
  76. }
  77.  
  78. //SecondActivity .java
  79. public class SecondActivity extends AppCompatActivity{
  80. private Button prevButton;
  81. @Override
  82. protected void onCreate(@Nullable Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. setContentView(R.layout.second_activity);
  85. prevButton = (Button) findViewById(R.id.prev_activity);
  86. prevButton.setOnClickListener(new View.OnClickListener() {
  87. @Override
  88. public void onClick(View v) {
  89. Intent intents = new Intent(SecondActivity.this,ThirdActivity.class);
  90. startActivity(intents);
  91. }
  92. });
  93.  
  94. }
  95.  
  96. @Override
  97. public void onBackPressed() {
  98. super.onBackPressed();
  99. Intent intent = new Intent();
  100. intent.setAction("com.navi.test");
  101. intent.putExtra("INFORMATION","SENT FROM SECOND ACTIVITY");
  102. sendBroadcast(intent);
  103. }
  104. }
  105.  
  106.  
  107. //ThirdActivity .java
  108. public class ThirdActivity extends AppCompatActivity implements IJRReciever{
  109. private MyReciever mReciever;
  110. private Button mBotton;
  111.  
  112. @Override
  113. protected void onCreate(@Nullable Bundle savedInstanceState) {
  114. super.onCreate(savedInstanceState);
  115. setContentView(R.layout.third_activity);
  116. mBotton = (Button) findViewById(R.id.fourth_act_btn);
  117. mBotton.setOnClickListener(new View.OnClickListener() {
  118. @Override
  119. public void onClick(View v) {
  120. if(mReciever == null){
  121. mReciever = new MyReciever();
  122. mReciever.setListener(ThirdActivity.this);
  123. /* IntentFilter filter = new IntentFilter();
  124. filter.addAction("com.navi.test");
  125. filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
  126. Log.d("SmsReceiver", this.getClass().getSimpleName() + " : " + "Register");
  127. registerReceiver(mReciever,filter);*/
  128.  
  129. Intent inte =new Intent(ThirdActivity.this,FourthActivity.class);
  130. startActivity(inte);
  131. }
  132. }
  133. });
  134.  
  135.  
  136.  
  137. }
  138.  
  139. @Override
  140. public void getMessageFromReciever(String message) {
  141. Toast.makeText(this,message,Toast.LENGTH_LONG).show();
  142. unregisterSmsReceiver();
  143. }
  144.  
  145.  
  146. private void unregisterSmsReceiver() {
  147. try {
  148. if (mReciever != null) {
  149. unregisterReceiver(mReciever);
  150. mReciever.setListener(null);
  151. mReciever = null;
  152. Log.d("SmsReceiver", this.getClass().getSimpleName() + " : " + "unregister");
  153. }
  154. } catch (Exception e) {
  155.  
  156. }
  157. }
  158. }
  159.  
  160. //FourthActivity .java
  161. public class FourthActivity extends AppCompatActivity{
  162. @Override
  163. protected void onCreate(@Nullable Bundle savedInstanceState) {
  164. super.onCreate(savedInstanceState);
  165. setContentView(R.layout.fourth_activity);
  166. }
  167.  
  168. @Override
  169. public void onBackPressed() {
  170. super.onBackPressed();
  171. Intent intent = new Intent();
  172. intent.setAction("com.navi.test");
  173. intent.putExtra("INFORMATION","SENT FROM FOURTH ACTIVITY");
  174. sendBroadcast(intent);
  175. }
  176. }
  177.  
  178. //MyReciever.java
  179. public class MyReciever extends BroadcastReceiver{
  180. private String message;
  181. private IJRReciever mListener;
  182.  
  183. public void setListener(IJRReciever listener){
  184. this.mListener = listener;
  185. }
  186.  
  187. @Override
  188. public void onReceive(Context context, Intent intent) {
  189. message = intent.getStringExtra("INFORMATION");
  190.  
  191. Toast.makeText(context,"Message Recieved in MyReciever",Toast.LENGTH_LONG).show();
  192.  
  193. //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
  194. if(mListener != null){
  195. mListener.getMessageFromReciever(message);
  196. }
  197. }
  198. }
  199.  
  200.  
  201. //AndroidMenifest.xml File
  202. <?xml version="1.0" encoding="utf-8"?>
  203. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  204. package="com.example.naveenbm.broadcastreciever">
  205.  
  206. <application
  207. android:allowBackup="true"
  208. android:icon="@mipmap/ic_launcher"
  209. android:label="@string/app_name"
  210. android:supportsRtl="true"
  211. android:theme="@style/AppTheme">
  212. <activity android:name=".MainActivity">
  213. <intent-filter>
  214. <action android:name="android.intent.action.MAIN" />
  215.  
  216. <category android:name="android.intent.category.LAUNCHER" />
  217. </intent-filter>
  218. </activity>
  219.  
  220. <activity android:name=".SecondActivity">
  221.  
  222. </activity>
  223.  
  224. <activity android:name=".ThirdActivity">
  225.  
  226. </activity>
  227.  
  228. <activity android:name=".FourthActivity">
  229.  
  230. </activity>
  231.  
  232. <receiver
  233. android:name=".MyReciever"
  234. android:enabled="true"
  235. android:exported="true" >
  236. <intent-filter>
  237. <action android:name="com.navi.test" />
  238. </intent-filter>
  239. </receiver>
  240.  
  241. </application>
  242.  
  243. </manifest>
  244.  
  245.  
  246. //activity_main.xml
  247. <?xml version="1.0" encoding="utf-8"?>
  248. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  249. xmlns:tools="http://schemas.android.com/tools"
  250. android:layout_width="match_parent"
  251. android:layout_height="match_parent"
  252. android:paddingBottom="@dimen/activity_vertical_margin"
  253. android:paddingLeft="@dimen/activity_horizontal_margin"
  254. android:paddingRight="@dimen/activity_horizontal_margin"
  255. android:paddingTop="@dimen/activity_vertical_margin"
  256. tools:context="com.example.naveenbm.broadcastreciever.MainActivity"
  257. android:gravity="center_horizontal">
  258.  
  259. <TextView
  260. android:id="@+id/info_text"
  261. android:layout_width="wrap_content"
  262. android:layout_height="wrap_content"
  263. android:text="Send Custom Braodcast"
  264. android:textColor="@color/black"
  265. />
  266.  
  267. <Button
  268. android:id="@+id/send_btn"
  269. android:layout_width="wrap_content"
  270. android:layout_height="wrap_content"
  271. android:text="Register BroadCast and Go to Next Activity"
  272. android:textSize="16sp"
  273. android:layout_below="@id/info_text"
  274. android:layout_marginTop="20dp"/>
  275.  
  276. <Button
  277. android:id="@+id/next_activity"
  278. android:layout_width="match_parent"
  279. android:layout_height="wrap_content"
  280. android:text="Next Activity"
  281. android:textColor="@color/black"
  282. android:textSize="16sp"
  283. android:layout_marginTop="20dp"
  284. android:layout_below="@+id/send_btn"/>
  285.  
  286. </RelativeLayout>
  287.  
  288.  
  289. //second_activity.xml
  290. <?xml version="1.0" encoding="utf-8"?>
  291. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  292. android:orientation="vertical" android:layout_width="match_parent"
  293. android:layout_height="match_parent">
  294.  
  295. <TextView
  296. android:layout_width="wrap_content"
  297. android:layout_height="wrap_content"
  298. android:text="Second Activity"
  299. android:layout_gravity="center_horizontal"
  300. android:textSize="16sp"
  301. android:textColor="@color/black"/>
  302.  
  303. <TextView
  304. android:id="@+id/text_val_sec"
  305. android:layout_width="wrap_content"
  306. android:layout_height="wrap_content"
  307. android:textSize="16sp"
  308. android:layout_margin="20dp"
  309. android:layout_gravity="center_horizontal"
  310. android:textColor="@color/black"/>
  311.  
  312.  
  313. <Button
  314. android:id="@+id/prev_activity"
  315. android:layout_width="wrap_content"
  316. android:layout_height="wrap_content"
  317. android:text="Go to Third Activity"
  318. android:textColor="@color/black"
  319. android:textSize="16sp"
  320. android:layout_gravity="center_horizontal"
  321. android:layout_marginTop="20dp"/>
  322.  
  323. </LinearLayout>
  324.  
  325. //third_activity.xml
  326. <?xml version="1.0" encoding="utf-8"?>
  327. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  328. android:orientation="vertical" android:layout_width="match_parent"
  329. android:layout_height="match_parent">
  330.  
  331. <TextView
  332. android:layout_width="wrap_content"
  333. android:layout_height="wrap_content"
  334. android:text="Third Activity"
  335. android:textSize="16sp"
  336. android:textColor="@color/black"/>
  337.  
  338. <Button
  339. android:id="@+id/fourth_act_btn"
  340. android:layout_width="wrap_content"
  341. android:layout_height="wrap_content"
  342. android:text="Go to Fourth Activity"
  343. android:textSize="16sp"
  344. android:textColor="@color/black"
  345. android:layout_marginTop="20dp"/>
  346.  
  347. </LinearLayout>
  348.  
  349. //fourth_activity.xml
  350. <?xml version="1.0" encoding="utf-8"?>
  351. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  352. android:orientation="vertical" android:layout_width="match_parent"
  353. android:layout_height="match_parent">
  354.  
  355. <TextView
  356. android:layout_width="wrap_content"
  357. android:layout_height="wrap_content"
  358. android:text="Fourth Activity"
  359. android:textSize="16sp"
  360. android:layout_gravity="center_horizontal"
  361. android:textColor="@color/black"/>
  362.  
  363. </LinearLayout>
Add Comment
Please, Sign In to add comment