[안드로이드강좌]브로드캐스트 리시버와 안드로이드 서비스를 이용한 예제

FSP 0 724 03.10 20:37

[안드로이드강좌]브로드캐스트 리시버와 안드로이드 서비스를 이용한 예제

 

출처 : http://www.truiton.com/2014/09/android-service-broadcastreceiver-example/

 

 

b8786114ddb34f9a21802c03602b8a77_1489146
 

 

[activity_main.xml]

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kr.co.topcredu.broadcastexamapplication.MainActivity">

<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:scrollbars="vertical" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

 

[MainActivity.java]

package kr.co.topcredu.broadcastexamapplication;

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.text.method.ScrollingMovementMethod;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
public static final String mBroadcastStringAction = "a.b.string";
public static final String mBroadcastIntegerAction = "a.b.integer";
public static final String mBroadcastArrayListAction = "a.b.arraylist";
private TextView mTextView;
private IntentFilter mIntentFilter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView1);
mTextView.setMovementMethod(new ScrollingMovementMethod());

mIntentFilter = new IntentFilter();
mIntentFilter.addAction(mBroadcastStringAction);
mIntentFilter.addAction(mBroadcastIntegerAction);
mIntentFilter.addAction(mBroadcastArrayListAction);

Intent serviceIntent = new Intent(this, BroadcastService.class);
startService(serviceIntent);
}

@Override
public void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mTextView.setText(mTextView.getText()
+ "Broadcast From Service: \n");
if (intent.getAction().equals(mBroadcastStringAction)) {
mTextView.setText(mTextView.getText()
+ intent.getStringExtra("Data") + "\n\n");
} else if (intent.getAction().equals(mBroadcastIntegerAction)) {
mTextView.setText(mTextView.getText().toString()
+ intent.getIntExtra("Data", 0) + "\n\n");
} else if (intent.getAction().equals(mBroadcastArrayListAction)) {
mTextView.setText(mTextView.getText()
+ intent.getStringArrayListExtra("Data").toString()
+ "\n\n");
Intent stopIntent = new Intent(MainActivity.this,
BroadcastService.class);
stopService(stopIntent);
}
}
};

@Override
protected void onPause() {
unregisterReceiver(mReceiver);
super.onPause();
}
}

 

[BroacastService.java]

package kr.co.topcredu.broadcastexamapplication;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import java.util.ArrayList;

public class BroadcastService extends Service {
private String LOG_TAG = null;
private ArrayList<String> mList;

@Override
public void onCreate() {
super.onCreate();
LOG_TAG = this.getClass().getSimpleName();
Log.i(LOG_TAG, "In onCreate");
mList = new ArrayList<String>();
mList.add("Object 1");
mList.add("Object 2");
mList.add("Object 3");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(LOG_TAG, "In onStartCommand");
new Thread(new Runnable() {
public void run() {
for (int i=0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MainActivity.mBroadcastStringAction);
broadcastIntent.putExtra("Data", "Broadcast Data");
sendBroadcast(broadcastIntent);

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
broadcastIntent.setAction(MainActivity.mBroadcastIntegerAction);
broadcastIntent.putExtra("Data", 10);
sendBroadcast(broadcastIntent);

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
broadcastIntent
.setAction(MainActivity.mBroadcastArrayListAction);
broadcastIntent.putExtra("Data", mList);
sendBroadcast(broadcastIntent);
}
}
}).start();
return START_REDELIVER_INTENT;
}

@Override
public IBinder onBind(Intent intent) {
// Wont be called as service is not bound
Log.i(LOG_TAG, "In onBind");
return null;
}

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.i(LOG_TAG, "In onTaskRemoved");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.i(LOG_TAG, "In onDestroy");
}
}

[AndroidManifest.xml]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.topcredu.broadcastexamapplication">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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=".BroadcastService"/>
</application>

</manifest>




 

Comments