I'm using ShotWatch to capture screenshots events.
The included example only works while inside the aplication, but I want to capture the event and launch an Activity. My code seems to ignore the service.
The mainfest has all the services/activity defined.
MainActivity:
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = findViewById(R.id.imageView);
// run-time permissions
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
// You can directly ask for the permission.
// The registered ActivityResultCallback gets the result of this request.
Toast.makeText(this, ":(", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// You can use the API that requires the permission.
Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
startService(new Intent(MainActivity.this, ScreenshotCaptureService.class));
}
}
}
ScreenshotCaptureService:
public class ScreenshotCaptureService extends Service implements ShotWatch.Listener {
private ShotWatch shotWatch;
@Override
public void onCreate() {
super.onCreate();
this.shotWatch = new ShotWatch(getContentResolver(), this);
this.shotWatch.register();
}
@Override
public void onDestroy() {
super.onDestroy();
this.shotWatch.unregister();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
@Override
public void onScreenShotTaken(ScreenshotData data) {
Intent intent = new Intent(this, AddActivity.class);
intent.putExtra(AddActivity.CAPTURE_PATH, data.getPath());
startService(intent);
}
}
AddActivity:
public class AddActivity extends AppCompatActivity {
public static final String CAPTURE_PATH = "ADD_CAPTURE_PATH";
private ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
Uri captura = Uri.fromFile(new File(getIntent().getStringExtra(AddActivity.CAPTURE_PATH)));
this.imageButton = findViewById(R.id.imageButton);
this.imageButton.setImageURI(captura);
this.imageButton.setOnClickListener((View v)->{
// ...
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) return;
switch (requestCode) {
// ...
}
}
}
Fixed.
I was using 'startService' instead of 'startActivity' inside onScreenShotTaken, on ScreenshotCaptureService.