티스토리 뷰

반응형

// Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pkg.Alram"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />
 
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AlramActivity"
                  android:label="@string/app_name" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="AlramEnd">
            <intent-filter>
                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>
        </activity>
        
        <service android:name=".AlramService" android:enabled="true">
<intent-filter>
<action android:name="pkg.Alram.AlramService" />
</intent-filter>
</service>
 
    </application>
    
    <uses-permission android:name="android.permission.VIBRATE"/> // 안 넣어주면 실행 안됨.
    
</manifest>
 
// main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
<Button 
  android:id="@+id/BtnAlramStart"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="낮잠 서비스 시작" 
/>
 
<Button 
  android:id="@+id/BtnAlramStop"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="낮잠 서비스 종료" 
/>
 
</LinearLayout>
 
// end.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
<TextView  
     android:layout_width="wrap_content" 
     android:layout_height="80px" 
     android:text="잘 잤니? 이제 일해야지 ㅋㅋ"     
     android:textSize="40px"
  />
 
</LinearLayout>
 
// AlramActivity.java
package pkg.Alram;
 
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class AlramActivity extends Activity {
static final  int  NAPNOTI = 1;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        findViewById(R.id.BtnAlramStart).setOnClickListener(mClick);
        findViewById(R.id.BtnAlramStop).setOnClickListener(mClick);
    }
    
    Button.OnClickListener mClick = new View.OnClickListener() {
     @Override
     public void onClick(View v) {
     Intent intent;
    
     switch(v.getId()) {
         case R.id.BtnAlramStart  : intent = new Intent(AlramActivity.this, AlramService.class);
          startService(intent);
                break;
              
         case R.id.BtnAlramStop  : intent = new Intent(AlramActivity.this, AlramService.class);
stopService(intent);
                break;
        }
     }
    };
}
 
// AlramEnd.java
package pkg.Alram;
 
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
 
public class AlramEnd extends Activity {
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.end);
    
    NotificationManager NM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    NM.cancel(AlramActivity.NAPNOTI);
}
 
}
 
 
// AlramService
package pkg.Alram;
 
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;
 
public class AlramService extends Service {
private boolean bThreadActive;
private NotificationManager mNotiManager;
 
public void onCreate() {
super.onCreate();
}
 
public void onDestroy() {
super.onDestroy();
 
mNotiManager.cancel(AlramActivity.NAPNOTI);
bThreadActive = false;
 
Toast.makeText(this, "낮잠 서비스 종료 !!", Toast.LENGTH_SHORT).show();
}
 
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
 
mNotiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
 
bThreadActive = true;
AlramThread thread = new AlramThread();
thread.start();
 
Toast.makeText(this, "낮잠 서비스 시작 !!", Toast.LENGTH_SHORT).show();
 
return START_STICKY;
}
 
public IBinder onBind(Intent intent){
return null; // Manifest에서 구현해줌
}
 
class AlramThread extends Thread {
public void run() {
while(bThreadActive) {
mHandler.sendEmptyMessage(0);
       
try { Thread.sleep(10000); }  // 10초에 한번 호출
catch(InterruptedException e) { ; }
}
    }
}
    
    Handler mHandler = new Handler() {
     public void handleMessage(Message msg) {
     if(msg.what == 0) {
     Notification noti = new Notification(R.drawable.icon, "일어나 임마!!", System.currentTimeMillis());
noti.defaults |= Notification.DEFAULT_SOUND;
// noti.defaults |= Notification.DEFAULT_VIBRATE; // 진동
 
// noti.flags |= Notification.FLAG_INSISTENT;
 
Intent intent = new Intent(AlramService.this, AlramEnd.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent content = PendingIntent.getActivity(AlramService.this, 0, intent, 0);
 
noti.setLatestEventInfo(AlramService.this, "기상 !!!!!", "일어나! 일할시간이야 ~~", content);
 
mNotiManager.notify(AlramActivity.NAPNOTI, noti);
     }
     }
    };
 
}
 
반응형

'Android > 실전 TIP' 카테고리의 다른 글

3 탭뷰 (아이콘)  (0) 2019.03.25
EditText OnChange 이벤트 처리  (0) 2019.03.25
ftp에서 파일 다운 받기  (0) 2019.03.25
ListView 특정 위치로 스크롤이 가게 하기  (0) 2019.03.25
스마트폰의 해상도 구하기  (0) 2019.03.25
댓글
반응형
최근에 올라온 글
Total
Today
Yesterday