Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.serwis;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.content.ContextCompat;
- import android.Manifest;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.view.WindowManager;
- import android.widget.Button;
- import android.content.Intent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- private final int PHYISCAL_ACTIVITY = 0;
- private Button btnStopService;
- private Button btnStartService;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if (ContextCompat.checkSelfPermission(this,
- Manifest.permission.ACTIVITY_RECOGNITION) == PackageManager.PERMISSION_DENIED) {
- //ask for permission
- requestPermissions(new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, PHYISCAL_ACTIVITY);
- }
- getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
- btnStartService = findViewById(R.id.btnStartService);
- btnStopService = findViewById(R.id.btnStopService);
- initButtonsOnClick();
- }
- private void initButtonsOnClick() {
- OnClickListener listener = new OnClickListener() {
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btnStartService:
- startMyService();
- break;
- case R.id.btnStopService:
- stopMyService();
- break;
- default:
- break;
- }
- }
- };
- btnStartService.setOnClickListener(listener);
- btnStopService.setOnClickListener(listener);
- }
- private void startMyService() {
- Intent serviceIntent = new Intent(this, MyService.class);
- startService(serviceIntent);
- }
- private void stopMyService() {
- Intent serviceIntent = new Intent(this, MyService.class);
- stopService(serviceIntent);
- }
- }
Add Comment
Please, Sign In to add comment