Example of IntentService and BroadcastReceiver - Apk Apps For you

Apk Apps For you

Foxi apk download latest version for Android,fifa 20 download for Android,fifa 20 download,mobile games,games download,Android games free download apk

Click here to download

Search This Blog

2 > 3 4

Wednesday, May 25, 2016

Example of IntentService and BroadcastReceiver


Last post show example of using Service and BroadcastReceiver, this example show how to do the same job using IntentService and BroadcastReceiver.


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidservice.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<Button
android:id="@+id/startservice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Service"/>
<EditText
android:id="@+id/msgtosend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="msg to send..." />
<Button
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send msg to service"/>
<TextView
android:id="@+id/cntreceived"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="26dp"/>
<TextView
android:id="@+id/msgreceived"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="bold"
android:textSize="20dp"/>

</LinearLayout>


MainActivity.java
package com.blogspot.android_er.androidservice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Button btnStart, btnSend;
EditText editTextMsgToSend;
TextView textViewCntReceived, textViewMsgReceived;

MyMainReceiver myMainReceiver;
Intent myIntent = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button)findViewById(R.id.startservice);
btnSend = (Button)findViewById(R.id.send);
editTextMsgToSend = (EditText)findViewById(R.id.msgtosend);
textViewCntReceived = (TextView)findViewById(R.id.cntreceived);
textViewMsgReceived = (TextView)findViewById(R.id.msgreceived);

btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService();
}
});

btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msgToService = editTextMsgToSend.getText().toString();

Intent intent = new Intent();
intent.setAction(MyIntentService.ACTION_MSG_TO_SERVICE);
intent.putExtra(MyIntentService.KEY_MSG_TO_SERVICE, msgToService);
sendBroadcast(intent);
}
});
}

private void startService(){
myIntent = new Intent(MainActivity.this, MyIntentService.class);
startService(myIntent);
}

@Override
protected void onStart() {
myMainReceiver = new MyMainReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyIntentService.ACTION_UPDATE_CNT);
intentFilter.addAction(MyIntentService.ACTION_UPDATE_MSG);
registerReceiver(myMainReceiver, intentFilter);
super.onStart();
}

@Override
protected void onStop() {
unregisterReceiver(myMainReceiver);
super.onStop();
}

@Override
protected void onDestroy() {
super.onDestroy();
}

private class MyMainReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(MyIntentService.ACTION_UPDATE_CNT)){
int int_from_service = intent.getIntExtra(MyIntentService.KEY_INT_FROM_SERVICE, 0);
textViewCntReceived.setText(String.valueOf(int_from_service));
}else if(action.equals(MyIntentService.ACTION_UPDATE_MSG)){
String string_from_service = intent.getStringExtra(MyIntentService.KEY_STRING_FROM_SERVICE);
textViewMsgReceived.setText(String.valueOf(string_from_service));
}
}
}
}


MyIntentService.java
package com.blogspot.android_er.androidservice;

import android.app.IntentService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Looper;
import android.widget.Toast;

public class MyIntentService extends IntentService {

//from MyIntentService to MainActivity
final static String KEY_INT_FROM_SERVICE = "KEY_INT_FROM_SERVICE";
final static String KEY_STRING_FROM_SERVICE = "KEY_STRING_FROM_SERVICE";
final static String ACTION_UPDATE_CNT = "UPDATE_CNT";
final static String ACTION_UPDATE_MSG = "UPDATE_MSG";

//from MainActivity to MyIntentService
final static String KEY_MSG_TO_SERVICE = "KEY_MSG_TO_SERVICE";
final static String ACTION_MSG_TO_SERVICE = "MSG_TO_SERVICE";

MyServiceReceiver myServiceReceiver;
int cnt;

public MyIntentService() {
super("MyIntentService");
}

@Override
public void onCreate() {
Toast.makeText(getApplicationContext(),
"onCreate", Toast.LENGTH_LONG).show();
myServiceReceiver = new MyServiceReceiver();
super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(),
"onStartCommand", Toast.LENGTH_LONG).show();

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_MSG_TO_SERVICE);
registerReceiver(myServiceReceiver, intentFilter);

return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
Toast.makeText(getApplicationContext(),
"onDestroy", Toast.LENGTH_LONG).show();
unregisterReceiver(myServiceReceiver);
super.onDestroy();
}

@Override
protected void onHandleIntent(Intent intent) {

String prompt;
//check if current thread is Main Thread (UI)
if(Looper.myLooper() == Looper.getMainLooper()){
prompt = "onHandleIntent run in UI Thread";
}else{
prompt = "onHandleIntent run in NOT UI Thread";
}

Intent iPrompt = new Intent();
iPrompt.setAction(ACTION_UPDATE_MSG);
iPrompt.putExtra(KEY_STRING_FROM_SERVICE, prompt);
sendBroadcast(iPrompt);

cnt = 10;
while (cnt >= 0){
try {
Thread.sleep(1000);

Intent i = new Intent();
i.setAction(ACTION_UPDATE_CNT);
i.putExtra(KEY_INT_FROM_SERVICE, cnt);
sendBroadcast(i);

cnt--;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public class MyServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

String action = intent.getAction();
if(action.equals(ACTION_MSG_TO_SERVICE)){
String msg = intent.getStringExtra(KEY_MSG_TO_SERVICE);

msg = new StringBuilder(msg).reverse().toString();

//send back to MainActivity
Intent i = new Intent();
i.setAction(ACTION_UPDATE_MSG);
i.putExtra(KEY_STRING_FROM_SERVICE, msg);
sendBroadcast(i);
}
}
}

}


Modify AndroidManifest.xml to add <service> of ".MyIntentService".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blogspot.android_er.androidservice">

<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>
<service android:name=".MyIntentService"/>
</application>

</manifest>


download filesDownload the files .

Related:
Android example of using Thread and Handler

No comments:

Post a Comment

Featured Post

Stumble Guys MOD APK 0.54.2

Popular Posts

Advertisement

2 > 3 4