안드로이드

 

 

 

 

AndoridManifest.xml

 


        <receiver
            android:name=".TestReceiver"
            android:enabled="true"
            android:exported="true">

            <!-- 인텐트 필터 - 암시적 호출에 사용됨  exam.test.receiver 이름을 임의 이름으로
                 호출시  임의 이름으로 호출 하면 된다.
                 즉 exam.test.receiver 를
                 intent.setAction("exam.test.receiver") 로
                 세팅 후 호출하면 .TestReceiver 가 실행 된다.
            -->
            <intent-filter>
                <action android:name="exam.test.receiver" />
            </intent-filter>
        </receiver>

 

 

class TestReceiver
package com.example.choi.ex10_service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class TestReceiver extends BroadcastReceiver {
    public TestReceiver() {
    }


    //브로드 캐스트를 수신할 때 호출되는 method
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "리시버가 호출되었습니다.", Toast.LENGTH_SHORT).show();


    }
}

 

 

class FilterActivity
package com.example.choi.ex10_service;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FilterActivity extends AppCompatActivity {

    Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);

        button1=(Button)findViewById(R.id.button1);


        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent =new Intent();
                //암시적 호출 (클래스 이름이 아닌  인텐트 필터의 이름으로 호출)
                intent.setAction("exam.test.receiver");
                sendBroadcast(intent);
            }
        });


    }
}

 

activity_filter.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:id="@+id/activity_filter"
    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.choi.ex10_service.FilterActivity">

    <Button
        android:text="리시버 호출"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

남의 다리 긁는다 , 애써서 해 놓은 일이 남을 위한 일이 되고 말았을 때 이르는 말.

댓글 ( 4)

댓글 남기기

작성