* 이해가 안됨.....쩝
<Main>
package com.oneplus.gubong_kim.service;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText edt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt = (EditText) findViewById(R.id.editText);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = edt.getText().toString();
Intent i = new Intent(getApplicationContext(), MyService.class);
i.putExtra("command", "show");
i.putExtra("name", name);
startActivity(i);
}
});
Intent passedIntent = getIntent();
processCommand(passedIntent);
}
private void processCommand(Intent passedIntent) {
if (passedIntent != null) {
String command = passedIntent.getStringExtra("command");
String name = passedIntent.getStringExtra("name");
Toast.makeText(this, "서비스로부터 전달받은 데이터 : " + command + ", " + name, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onNewIntent(Intent intent) {
processCommand(intent);
super.onNewIntent(intent);
}
}
<MyService>
package com.oneplus.gubong_kim.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
public class MyService extends Service {
private static final String Tag = "MyService";
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d(Tag, "onCreate() 호출됨");
}
@Override
//service에서는 intent의 정보를 확인 할 때 onStartCommand를 사용한다.
//즉 intent 명령어를 처리할 때 사용
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(Tag, "onStartCommand() 호출됨");
if (intent == null) {
return Service.START_STICKY;
} else {
processCommand(intent);
}
return super.onStartCommand(intent, flags, startId);
}
private void processCommand(Intent intent) {
String command = intent.getStringExtra("command");
String name = intent.getStringExtra("name");
Log.d(Tag, "전달받은 데이터 : " + command + ", " + name);
//화면이 없는 서비스에서 화면이 있는 곳으로 정보 띄우기
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent showIntent = new Intent(getApplicationContext(), MainActivity.class);
//화면이 없으니 뭔가를 만들어 줘야한다.
showIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_SINGLE_TOP
Intent.FLAG_ACTIVITY_CLEAR_TOP);
showIntent.putExtra("command", "show");
showIntent.putExtra("name", name + " from service");
startActivity(showIntent);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(Tag, "onDestroy() 호출됨");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
'노트필기' 카테고리의 다른 글
[Android] 2017.06.20(오후) (0) | 2017.06.20 |
---|---|
[Android] 2017.06.20(오전) (0) | 2017.06.20 |
[Do it 안드로이드] Parcelable 예제 (0) | 2017.06.18 |
[Android] 5th Class (0) | 2017.06.13 |
[Android] 4th Class (0) | 2017.06.12 |