서비스에서 전송하는 부분

Intent myFilteredResponse = new Intent("passData");
myFilteredResponse.putExtra("serviceData"msg);

넘겨 받는 부분

Intent intent = new Intent("passData");
String passData = intent.getStringExtra("serviceData");

하면 passData에 데이터가 들어가게 된다






서버에 execute로 데이터를 전송한 후에 리턴을 받고 싶은 경우가 있다.

그럴 경우에는 간단하게
urlConnect.execute().get();
뒤에 .get(); 을 써주면 된다.

그러면 오류가 뜨는데 그럴땐 Alt + Enter를 눌러서
try/catch 문을 자동으로 넣어주도록 한다.




기본적으로 onStartCommand의 RETURN 값으로 설정하는 것이다.

START_STICKY: 
Service가 강제 종료되었을 경우 시스템이 다시 Service를 재시작 시켜 주지만 intent 값을 null로 초기화 시켜서 재시작 합니다. Service 재시작시 intent 값이 null로 초기화 되서 재시작 됩니다.

START_NOT_STICKY:
이 Flag를 리턴해 주시면, 강제로 종료 된 Service가 재시작 하지 않습니다.
시스템에 의해 강제 종료되어도 괸찮은 작업을 진행 할 때 사용해 주시면 됩니다.

START_REDELIVER_INTENT:
START_STICKY와 마찬가지로 Service가 종료 되었을 경우 시스템이 다시 Service를 재시작 시켜 주지만 intent 값을 그대로 유지 시켜 줍니다.





// 모델 번호
Build.BOARD
// 브렌드
Build.BRAND
// CPU 정보
Build.CPU_ABI
// 디바이스 ( 모델 번호 )
Build.DEVICE
// 디스플레이 정보
Build.DISPLAY
// -----
Build.FINGERPRINT
// IP 주소
Build.HOST
// 디바이스 고유 아이디
Build.ID
// 제조사
Build.MANUFACTURER
// 모델 번호
Build.MODEL
// 상품명 ( 모델 번호 )
Build.PRODUCT
// ----
Build.TAGS
// 유저 타입
Build.TYPE
// 사용 유저의 정보
Build.USER
// 버전 정보
Build.VERSION.RELEASE




private final int PERMISSIONS_REQUEST_RESULT = 1;

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

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_EXTERNAL_STORAGE)) {
//권한을 거절하면 재 요청을 하는 함수
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_RESULT);
}
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (PERMISSIONS_REQUEST_RESULT == requestCode) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "권한 요청이 됐습니다.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "권한 요청을 해주세요.", Toast.LENGTH_SHORT).show();
finish();
}
return;
}
}

그리고 매니페스트에

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

추가 해주면 권한 요청이 완료된다.




가끔 페이지 전환을 할경우 안되는 경우도 있다.

그럴때 매니페스트에 Activity를 적용해줘야한다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.eunice.myapplication">

<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    //여기에 추가를 해주면 된다.
</application>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>



최종 코드
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.eunice.myapplication">

<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".LoginPage" />
</application>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>




처음에 생성되있는 java가 MainActivity.java라고 지정하고
페이지 전환해야될 곳이 LoginPage.java라고 지정한다.

public class MainActivity extends AppCompatActivity {

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

        Button login = (Button)findViewById(R.id.login_button); //login 변수에 버튼 아이디 추가
        login.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
            Intent intent = new Intent(
                getApplicationContext(),
                LoginPage.class // LoginPage의 클래스 추가
            );
            startActivity(intent); // Activity 실행
        }
    });
}

+ Recent posts