Skip to content

Instantly share code, notes, and snippets.

@pjhjohn
Created January 6, 2017 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjhjohn/a33ba34eb86104748f6a55bfd376d1cd to your computer and use it in GitHub Desktop.
Save pjhjohn/a33ba34eb86104748f6a55bfd376d1cd to your computer and use it in GitHub Desktop.
Minimal Permission Example
package ly.soundl.test.batterytester;
import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
public class MainActivity extends AppCompatActivity {
public static final String TAG = MainActivity.class.getSimpleName();
private static final int REQUEST_CODE_RECORD_AUDIO = 0x10;
private final Messenger mMessenger = new Messenger(new IncomingHandler());
/* Auto-Generated Method Stub */
@BindView(R.id.label) protected TextView mTextViewLabel;
@BindView(R.id.btn_start_service) protected Button mBtnStartService;
@BindView(R.id.btn_stop_service) protected Button mBtnStopService;
@BindView(R.id.toolbar) protected Toolbar toolbar;
private Unbinder mUnbinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
mUnbinder = ButterKnife.bind(this);
this.setSupportActionBar(toolbar);
/* Declare Target Service */
ComponentName componentName = new ComponentName(MainService.class.getPackage().getName(), MainService.class.getCanonicalName());
Intent intent = new Intent();
intent.setComponent(componentName);
/* Request Mic Permission only */
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
mTextViewLabel.setText(R.string.permission_granted);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_CODE_RECORD_AUDIO);
}
/* Start & Bind Service */
mBtnStartService.setOnClickListener(view -> {
this.startService(intent);
this.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
Toast.makeText(this, "Service successfully binded", Toast.LENGTH_SHORT).show();
});
/* Unbind & Stop Service */
mBtnStopService.setOnClickListener(view -> {
try {
this.unbindService(mServiceConnection);
Toast.makeText(this, "Service successfully unbinded", Toast.LENGTH_SHORT).show();
} catch (IllegalArgumentException e) {
Toast.makeText(this, "Running Service Not Found", Toast.LENGTH_SHORT).show();
}
this.stopService(intent);
});
}
@Override
protected void onDestroy() {
super.onDestroy();
mUnbinder.unbind();
}
ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected : " + name.toShortString() + " on " + service.toString());
Messenger messenger = new Messenger(service);
Message msg = Message.obtain(null, 1);
msg.replyTo = mMessenger;
try {
messenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected : " + name.toShortString());
}
};
private static class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// Actually, does nothing since super has empty body
super.handleMessage(msg);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_RECORD_AUDIO: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
mTextViewLabel.setText(R.string.permission_granted);
} else {
// Permission Denied. Disabling the functionality depends on the permission
mTextViewLabel.setText(R.string.permission_denied);
mBtnStartService.setEnabled(false);
mBtnStopService.setEnabled(false);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment