안드로이드

 

 

 

 

 

 

 

 

class MainActivity
package com.example.choi.mystudy24;

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

public class MainActivity extends AppCompatActivity {

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

        Button button=(Button)findViewById(R.id.bt_broadcast);

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

                Intent intent=new Intent();
                //메인 엑티비티 receiver 의 action 이름과 같아야 한다.
                intent.setAction("com.example.choi.mystudy24.BROADCAST");
                sendBroadcast(intent);

            }
        });

    }

}

 

R.layout.activity_main
<?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_main"
    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.mystudy24.MainActivity">

    <Button
        android:text="BroadCast"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="65dp"
        android:id="@+id/bt_broadcast"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@android:color/holo_green_dark"
        android:textColor="@android:color/background_light" />
</RelativeLayout>

 

class BrEx
package com.example.choi.mystudy24;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by choi on 2017-03-10.
 */

public class BrEx extends BroadcastReceiver {

    final static String TAG="BrEX";
/*
    final static String STRING_POWER_CONNECTED="android.intent.action.ACTION_POWER_CONNECTED";
    final static String STRING_POWER_DISCONNECTED="android.intent.action.ACTION_POWER_DISCONNECTED";
*/


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

        Log.i(TAG, "onReceive()");

        Intent intent = new Intent(context, WorkActivity.class);
        intent.putExtra("strName", "strValue");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }






}

 

class WorkActivity
package com.example.choi.mystudy24;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class WorkActivity extends AppCompatActivity {

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

        Intent intent=getIntent();
        String string=intent.getStringExtra("strName");

        TextView textView=(TextView)findViewById(R.id.tv_result);
        textView.setText(string);

    }



}

 

 

R.layout.work
<?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/work"
    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.mystudy24.WorkActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="102dp"
        android:id="@+id/tv_result"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textAlignment="center"
        android:textStyle="normal|bold"
        android:textSize="30sp"
        android:textColor="@android:color/holo_orange_dark" />
</RelativeLayout>

 

 

AndroidMainfest.xml


        <receiver android:name="com.example.choi.mystudy24.BrEx">
            <intent-filter>
                <!--임의 이름-->
                <action android:name="com.example.choi.mystudy24.BROADCAST"/>
            </intent-filter>
        </receiver>

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

남의 결점을 지적하더라도 결코 듣기 싫은 말은 사랑으로써 해야 할 것입니다. -안창호

댓글 ( 4)

댓글 남기기

작성