안드로이드

 

 

 

 

 

class MainActivity
package com.example.choi.mystudy20alarm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    final  static String TAG="MainActivity";
    Button btnOneAlarm, btnCycleAlarm, btnStopAlarm;
    AlarmManager alarmManager;

    Intent intent;
    PendingIntent pendingIntent;


    Toast toast;

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

        btnOneAlarm=(Button)findViewById(R.id.btnOneAlarm);
        btnCycleAlarm=(Button)findViewById(R.id.btnCycleAlarm);
        btnStopAlarm=(Button)findViewById(R.id.btnStopAlarm);

        alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);

        btnOneAlarm.setOnClickListener(listener);
        btnCycleAlarm.setOnClickListener(listener);
        btnStopAlarm.setOnClickListener(listener);

    }

    public void intentSet(){
        intent=new Intent(MainActivity.this, SecondActivity.class);
        pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

    }


    private void oneAlarm(){
        //3초 후 알람
        intentSet();
        alarmManager.set(AlarmManager.RTC, System.currentTimeMillis()+3000, pendingIntent);

    }


    private void cycleAlarm(){
       // 1초마다 반복
       intentSet();
       alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + 3000, 1000, pendingIntent);
       Toast.makeText(MainActivity.this, "1초마다 반복", Toast.LENGTH_SHORT).show();
    }


    View.OnClickListener listener=new View.OnClickListener() {



        @Override
        public void onClick(View v) {
            Log.i(TAG, "onClick v.getId() : " + v.getId());
            switch (v.getId()){
                case R.id.btnOneAlarm:
                    oneAlarm();
                    break;

                case R.id.btnCycleAlarm:
                    cycleAlarm();
                    break;

                case R.id.btnStopAlarm:

                    intentSet();
                    alarmManager.cancel(pendingIntent);
                    toastShow("알람종료");
                    break;

                default:
                    break;
            }
        }
    };



    private void toastShow(String message){
        if(toast==null){
            toast=Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT);
        }else {
            toast.setText(message);
        }
        toast.show();
    }



}



 

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.mystudy20alarm.MainActivity">


    <Button
        android:text="One Alarm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/btnOneAlarm"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textAllCaps="false"
        android:background="@android:color/holo_blue_dark"
        android:textColor="@android:color/background_light" />

    <Button
        android:text="Stop Cycle Alarm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnStopAlarm"
        android:textAllCaps="false"
        android:background="@android:color/holo_red_light"
        android:textColor="@android:color/background_light"
        android:layout_marginTop="56dp"
        android:layout_below="@+id/btnCycleAlarm"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="Cycle Alarm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnCycleAlarm"
        android:textAllCaps="false"
        android:background="@android:color/holo_orange_dark"
        android:textColor="@android:color/background_light"
        android:layout_below="@+id/btnOneAlarm"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="66dp" />
</RelativeLayout>

 

class SecondActivity
package com.example.choi.mystudy20alarm;

import android.app.AlarmManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {


    AlarmManager alarmManager;


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

        alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE);

    }


}

 

R.layout.second_ativity
<?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/second_ativity"
    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.mystudy20alarm.SecondActivity">

    <TextView
        android:text="알람중"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:id="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:textStyle="normal|bold"
        android:textAlignment="center" />
</RelativeLayout>

 

 

 

 

 

 

 

android

 

about author

PHRASE

Level 60  머나먼나라

민심을 파악하는 데 성공하면 나라는 다스려지고 민심을 알지 못하면 나라는 어지러워진다. -묵자

댓글 ( 4)

댓글 남기기

작성