I can listen volume level in my VolumeService. This service listen to volume level in background.
My problem is: I can't control volume in lock screen.
I tried two different ways for volume control
First Way with CCL
Application Class:
public class CastApplication extends Application {
private static String APPLICATION_ID;
public static final double VOLUME_INCREMENT = 0.03;
#Override
public void onCreate() {
super.onCreate();
APPLICATION_ID = getString(R.string.app_id);
// initialize VideoCastManager
VideoCastManager.initialize(this, APPLICATION_ID, VideoCastControllerActivity.class, null).
setVolumeStep(VOLUME_INCREMENT).enableFeatures(VideoCastManager.FEATURE_NOTIFICATION |VideoCastManager.FEATURE_LOCKSCREEN |
VideoCastManager.FEATURE_WIFI_RECONNECT | VideoCastManager.FEATURE_CAPTIONS_PREFERENCE |VideoCastManager.FEATURE_DEBUGGING);
}
}
MainActivity:
public class MainActivity extends FragmentActivity {
private VideoCastManager mCastManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoCastManager.checkGooglePlayServices(this);
mCastManager = VideoCastManager.getInstance();
Intent i1 = new Intent(this,VolumeService.class);
startService(i1);
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (mCastManager.onDispatchVolumeKeyEvent(event, CastApplication.VOLUME_INCREMENT)) {
return true;
}
return super.dispatchKeyEvent(event);
}
}
Second way without CCL (with RRC)
MainActivity:
public class MainActivity extends FragmentActivity {
ContentObserver mSettingsContentObserver;
RemoteControlClient remoteControlClient;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_RING,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
RemoteControlClientCompat mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this.getApplicationContext(), 0,intent, 0));
RemoteControlHelper.registerRemoteControlClient(mAudioManager,mRemoteControlClientCompat);
remoteControlClient = new RemoteControlClient((PendingIntent.getBroadcast(this.getApplicationContext(), 0,intent, 0)));
MediaRouter.getInstance(this.getApplicationContext()).addRemoteControlClient(remoteControlClient);
Intent i1 = new Intent(this,VolumeService.class);
startService(i1);
}
}
Service and ContentObserver are same for both way
VolumeService:
public class VolumeService extends Service {
ContentObserver mSettingsContentObserver;
#Override
public void onCreate() {
super.onCreate();
mSettingsContentObserver = new SettingsContentObserver(this, new Handler());
getApplicationContext().getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI, true,mSettingsContentObserver);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
}
}
ContentObserver:
public class SettingsContentObserver extends ContentObserver {
int previousVolume;
Context context;
public SettingsContentObserver(Context c, Handler handler) {
super(handler);
context = c;
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
previousVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
}
#Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
#Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int delta = previousVolume - currentVolume;
if (delta > 0) {
Log.i("MyService", "Volume is down");
previousVolume = currentVolume;
} else if (delta < 0) {
Log.i("MyService", "Volume is up");
previousVolume = currentVolume;
}
}
}
Manifest:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:name="com.yns.volumelock.CastApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.google.android.libraries.cast.companionlibrary.remotecontrol.VideoIntentReceiver" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
</intent-filter>
</receiver>
<service
android:name="com.google.android.libraries.cast.companionlibrary.notification.VideoCastNotificationService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.notificationvisibility" />
</intent-filter>
</service>
<service android:name="com.google.android.libraries.cast.companionlibrary.cast.reconnection.ReconnectionService" />
<activity
android:name="com.google.android.libraries.cast.companionlibrary.cast.tracks.CaptionsPreferenceActivity"
android:label="#string/action_settings" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service
android:name="com.yns.volumelock.VolumeService"
android:enabled="true" >
</service>
</application>
Related
I'm trying to make an auto click app with AccessibilityService. I use method dispatchGesture with a AccessibilityService.GestureResultCallback to perform click on the screen and repeat it in onCompleted(). The problem is if I touch the screen before gesture completed, the gesture cancelled (onCancelled fired). How could I prevent screen touch stop my gesture?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.falcon.autoclick">
<application
android:name=".base.App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AutoClick">
<activity android:name=".ManageConfigActivity" />
<activity android:name=".TestActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".FloatingMenu"
android:enabled="true"
android:exported="false" />
<service
android:name=".AutoService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/global_action_bar_service" />
</service>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
#xml/global_action_bar_service
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canPerformGestures="true"
android:description="#string/app_name"
android:notificationTimeout="100"
android:settingsActivity=".MainActivity" />
AutoService.java
public static final String EXTRA_ACTION = "auto_service_action";
public static final String ACTION_START_AUTO = "start_auto_click";
public static final String ACTION_STOP_AUTO = "stop_auto_click";
public static final String EXTRA_TARGET = "extra_target";
private Handler handler;
private IntervalRunnable runnable;
private CountDownTimer startTimer;
private CountDownTimer endTimer;
#Override
public void onCreate() {
super.onCreate();
HandlerThread handlerThread = new HandlerThread("auto-handler");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if (action != null) {
if (action.equals(ACTION_START_AUTO)) {
Target target = (Target) intent.getSerializableExtra(EXTRA_TARGET);
runnable = new IntervalRunnable(target);
handler.postDelayed(runnable, 100);
} else if (action.equals(ACTION_STOP_AUTO)) {
stopAuto();
}
}
return super.onStartCommand(intent, flags, startId);
}
private void stopAuto() {
if (startTimer != null) {
startTimer.cancel();
}
if (endTimer != null) {
endTimer.cancel();
}
handler.removeCallbacks(runnable);
runnable = null;
}
private void startSingleTarget(Target target) {
dispatchGesture(getGestureDescription(target), new AccessibilityService.GestureResultCallback() {
#Override
public void onCompleted(GestureDescription gestureDescription) {
super.onCompleted(gestureDescription);
LogUtil.d("stopwatch", "gesture completed");
startTimer = new CountDownTimer(target.getDelayTime(), 1000) {
#Override
public void onTick(long l) {
}
#Override
public void onFinish() {
handler.post(runnable);
}
}.start();
}
#Override
public void onCancelled(GestureDescription gestureDescription) {
super.onCancelled(gestureDescription);
LogUtil.d("testttttt", "gesture cancelled");
}
}, null);
}
private GestureDescription getGestureDescription(Target target) {
long duration = 0;
GestureDescription.Builder gestureBuilder = new GestureDescription.Builder();
Path path = new Path();
path.moveTo(target.getCenterStartX(), target.getCenterStartY());
if (target.isSwipe()) {
path.lineTo(target.getCenterEndX(), target.getCenterEndY());
duration = target.getSwipeDuration();
} else {
duration = target.getClickDuration();
}
gestureBuilder.addStroke(new GestureDescription.StrokeDescription(path,
0,
duration));
return gestureBuilder.build();
}
private class IntervalRunnable implements Runnable {
private Target target;
public IntervalRunnable(Target target) {
this.target = target;
}
#Override
public void run() {
startSingleTarget(target);
}
}
How could I prevent screen touch stop my gesture?
You can't do that by dispatchGesture. From the official documentation about dispatchGesture: Dispatch a gesture to the touch screen. Any gestures currently in progress, whether from the user, this service, or another service, will be cancelled. Thus, gestures dispatched by your service can be canceled by user.
I am looking to have some basic forms run on my android device after it is reset or on the first boot like most smartphones ask you to connect to Wi-Fi, register the device etc. I haven't worked with native android development previously so what are my options and what areas should I look into?
Try this
BootCompleteReceiver.java
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MsgPushService.class);
context.startService(service);
}
}
MyService.java
public class MyService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
MainActivity
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(getBaseContext(), MyService.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newbootservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:name=".MyService"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I have this application i want to install it (it is service) but i don't have an activity so i can't install it on my device to try it can someone help me please i guess this is livewallpaper for photo sequence like slideshow so can anyone tell me what should i do?
public class MainActivity extends WallpaperService{
Handler handler;
private boolean visible;
public void onCreate()
{
super.onCreate();
}
public void onDestroy()
{
super.onDestroy();
}
public Engine onCreateEngine()
{
return new CercleEngine();
}
class CercleEngine extends Engine
{
public Bitmap image1, image2, image3;
CercleEngine()
{
image1 = BitmapFactory.decodeResource(getResources(), R.drawable.greenww);
image2 = BitmapFactory.decodeResource(getResources(), R.drawable.redww);
image3 = BitmapFactory.decodeResource(getResources(), R.drawable.screen3);
}
public void onCreate(SurfaceHolder surfaceHolder)
{
super.onCreate(surfaceHolder);
}
public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels)
{
drawFrame();
}
void drawFrame()
{
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try
{
c = holder.lockCanvas();
if (c != null)
{
c.drawBitmap(image1, 0, 0, null);
c.drawBitmap(image2, 0, 0, null);
c.drawBitmap(image3, 0, 0, null);
}
} finally
{
if (c != null) holder.unlockCanvasAndPost(c);
}
handler.removeCallbacks(drawRunner);
if (visible)
{
handler.postDelayed(drawRunner, 1000); // delay 1 sec
}
}
private final Runnable drawRunner = new Runnable()
{
#Override
public void run() {
drawFrame();
}
};
}
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mike.wallpaper2">
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service
android:enabled="true"
android:name=".MainActivity">
<intent-filter>
<action
android:name = "me.myapp.MyAppService">
</action>
</intent-filter>
</service>
<receiver android:enabled="true"
android:name=".BootUpReceiver">
<intent-filter> <action
android:name = "android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
BootUpReceiver
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
/***** For start Service ****/
Intent myIntent = new Intent(context, MainActivity.class);
context.startService(myIntent);
}
add/update this code in manifest
<service
android:enabled="true"
android:name=".MainActivity">
<intent-filter>
<action
android:name = "com.example.mike.wallpaper2.MainActivity">
</action>
</intent-filter>
</service>
<receiver
android:enabled="true"
android:name=".BootReceiver">
<intent-filter>
<action android:name = "android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
also add one new class
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MainActivity.class);
context.startService(service);
}
}
then follow steps mentioned in following example Start android application without activity
or restart your device this service start running
From EditConfiguration LUNCH OPTION (NOTHING)
I am a newbie for android. I have registered receiver in manifest using android.intent.action.DATE_CHANGED and it is working fine when a user changes the date manually. BUT my requirement is receiver has to be called automatically everyday the system date change say at 12:00 am. The user should not change the date.
Thanks in advance.
my code - manifest file
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NotificationOne"
android:label="#string/title_activity_notification_one"
android:parentActivityName=".FirstActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".FirstActivity" />
</activity>
<service android:name=".MyService">
</service>
<receiver android:name=".MyBroadcastreceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<receiver android:name=".SecondBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.DATE_CHANGED"/>
</intent-filter>
</receiver>
</application>
Broadcast Receiver class
public class SecondBroadcastReceiver extends BroadcastReceiver {
private Context mContext;
#Override
public void onReceive(Context context, Intent intent) {
mContext = context;
Toast.makeText(context,"inside second",Toast.LENGTH_SHORT).show();
MyBroadcastreceiver.firstConnect = true;
MyBroadcastreceiver.hasInternet = isInternetAvailable();
if(MyBroadcastreceiver.hasInternet) {
context.startService(new Intent(context, MyService.class));
MyBroadcastreceiver.firstConnect = false;
}
}
public boolean isInternetAvailable() {
Toast.makeText(mContext,"inside isInternetAvailable",Toast.LENGTH_SHORT).show();
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
one service- which has notification
public class MyService extends Service {
private NotificationManager myNotificationManager;
private int notificationIdOne = 111;
private int numMessagesOne = 0;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
displayNotificationOne();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
i have another receiver which will be called when internet is connected/disconnected.
hy..i have a task to make my kitkat-nexus to act as a tag. I have ACS 122U as reader. i have read the program example in this site http://blog.opendatalab.de/hack/2013/11/07/android-host-card-emulation-with-acr122/. then i tryed the code on my own eclipse.
main activity :
public class MainActivity extends Activity implements OnMessageReceived, ReaderCallback {
private NfcAdapter nfcAdapter;
private ListView listView;
private IsoDepAdapter isoDepAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
isoDepAdapter = new IsoDepAdapter(getLayoutInflater());
listView.setAdapter(isoDepAdapter);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
Log.i("end of onCreate-----","onCreate HCE");
}
#Override
public void onResume() {
super.onResume();
//nfcAdapter.enableReaderMode(this, this, NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK,
// null);
//nfcAdapter.disableReaderMode(this); //tambahan poipo
Log.i("onResume---", "onResume");
}
#Override
public void onPause() {
super.onPause();
nfcAdapter.disableReaderMode(this);
Log.i("onPause---", "onPause");
}
#Override
public void onTagDiscovered(Tag tag) {
IsoDep isoDep = IsoDep.get(tag);
IsoDepTransceiver transceiver = new IsoDepTransceiver(isoDep, this);
Thread thread = new Thread(transceiver);
Log.i("dibawah thread", "ontagdiscovered");
thread.start();
}
#Override
public void onMessage(final byte[] message) {
runOnUiThread(new Runnable() {
#Override
public void run() {
isoDepAdapter.addMessage(new String(message));
Log.i("didlmrun---", "onMessage");
}
});
Log.i("diluarrun---", "onMessage");
}
#Override
public void onError(Exception exception) {
onMessage(exception.getMessage().getBytes());
}
}
hostapduservice :
...
...
...
#Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
if (selectAidApdu(apdu)) {
Log.i("HCEDEMO====", "Application selected====");
return getWelcomeMessage();
}
else {
Log.i("HCEDEMO======", "Received: =====" + new String(apdu));
return getNextMessage();
}
}
...
...
...
then in the manifest file :
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="FEATURE_NFC_HOST_CARD_EMULATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name=".MyHostApduService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE" >
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="#xml/apduservice" />
</service>
<activity
android:name="de.grundid.hcedemo.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
ok,,when i ran the above source code,, i saw my acs122u blinking continously when i tapped my nexus near to it. but i didn't see the log.i(....) from hostapdu service. In the eclipse log cat, there were just some log.i from main activity. what should i do to bind that hostapdu service to main activity, so my nexus can act as a tag...???
thanks in advance... :-)