today I updated a google play services lib into an app for a newer version. Eclipse is not giving me any kind of errors, I compiled it and when I test on my phone works fine then I go to the menu and select exit and confirm with a yes to close it and the app disappears from screen.
But when you hit the square at the bottom of the phone (when you list all opened apps) the app is still there running! o_O
Prior to the update the app was closing fine and now remains active after exit. But I dont think that gps could cause this kind of issue. Any suggestion???
Here is my MainActivity.java (where the exit menu process is executed)
package com.myapp.radio;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import com.myapp.radio.R;
public class BaseActivity extends Activity {
private Intent bindIntent;
private RadioService radioService;
private static boolean isExitMenuClicked;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isExitMenuClicked = false;
// Bind to the service
bindIntent = new Intent(this, RadioService.class);
bindService(bindIntent, radioConnection, Context.BIND_AUTO_CREATE);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
#Override
protected void onResume() {
super.onResume();
if (isExitMenuClicked == true)
finish();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
if (radioService.getTotalStationNumber() <= 1) {
}
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent i;
final String thisClassName = this.getClass().getName();
final String thisPackageName = this.getPackageName();
if (item.getItemId() == R.id.radio) {
if (!thisClassName.equals(thisPackageName + ".MainActivity")) {
i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true;
}
} else if (item.getItemId() == R.id.exit) {
String title = "Cerrar aplicación";
String message = "¿Realmente desea salir?";
String buttonYesString = "Si";
String buttonNoString = "No";
isExitMenuClicked = true;
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setTitle(title);
ad.setMessage(message);
ad.setCancelable(true);
ad.setPositiveButton(buttonYesString,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (radioService != null) {
radioService.exitNotification();
radioService.stop();
radioService.stopService(bindIntent);
isExitMenuClicked = true;
finish();
}
}
});
ad.setNegativeButton(buttonNoString, null);
ad.show();
return true;
} else if (item.getItemId() == R.id.about) {
if (!thisClassName.equals(thisPackageName + ".AboutActivity")) {
i = new Intent(this, AboutActivity.class);
startActivity(i);
return true;
}
} else if (item.getItemId() == R.id.facebook) {
if (!thisClassName.equals(thisPackageName + ".FacebookActivity")) {
i = new Intent(this, FacebookActivity.class);
startActivity(i);
return true;
}
} else if (item.getItemId() == R.id.twitter) {
if (!thisClassName.equals(thisPackageName + ".TwitterActivity")) {
i = new Intent(this, TwitterActivity.class);
startActivity(i);
return true;
}
}
return super.onOptionsItemSelected(item);
}
// Handles the connection between the service and activity
private final ServiceConnection radioConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
radioService = ((RadioService.RadioBinder) service).getService();
}
#Override
public void onServiceDisconnected(ComponentName className) {
radioService = null;
}
};
protected void onCreate1(Bundle savedInstanceState) {
// TODO Auto-generated method stub
}
}
This behavior is not related to Google Play Services, this is how Android works.
When you leave any application it's still alive for some time. There are no any running Activities, but Application instance is active. And if you launch it again after short time - it's started pretty quick, because some application data inherited from previous run. For example, all the static variables - they are still alive and contains previous values. It can cause very complex bugs at the restarted application, so be aware of that!
That extra living time depends on current conditions. If there are enough memory - it's longer, not enough - killed sooner.
Related
I am trying to create a simple App Lock for Android App. So I created app lock service like this
package com.company.applock;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Intent;
import android.content.Context;
import android.os.IBinder;
import android.util.Log;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.company.applock.LockscreenActivity;
public class LockService extends Service {
private static boolean isLock = false;
private Timer timer;
// Static Getters and setters
public static boolean isLock() {
return isLock;
}
public static void setLock(boolean lock) {
isLock = lock;
}
// Public static method to start service
public static void start(Context context) {
Intent intent = new Intent(context, LockService.class);
context.startService(intent);
}
// Overrides
#Override
public IBinder onBind(Intent intent) { return null; }
#Override
public void onCreate() {
timer = new Timer("LockService");
timer.schedule(checkLockTask, 500L, 500L);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (null != intent) { }
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
timer.cancel(); timer = null;
}
// Custom methods
private boolean isForegroundLocked() {
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
Log.d("topActivity", taskInfo.get(0).topActivity.getClassName());
return taskInfo.get(0).topActivity.getClassName().equals("com.company.applock.LockscreenActivity");
}
private void showLockActivity() {
Intent intent = new Intent(getApplicationContext(), LockscreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Log.d("showLockActivity", "success");
}
private TimerTask checkLockTask = new TimerTask() {
#Override
public void run() {
// TODO: Fix foreground bug
if (isLock) {
Log.d("isLock", "true");
if (isForegroundLocked()) {
Log.d("isForegroundLocked", "true");
} else {
Log.d("isForegroundLocked", "false");
showLockActivity();
}
} else {
Log.d("isLock", "false");
// TODO: Close lock screen
}
}
};
}
As you see, this is very simple one. The problem is;
When I press home button and get to launcher, it locks after a few seconds (generally 4 or 5 seconds)
If I remove my main activity (this is not lockscreen activity) from recent tasks, the service stops (it doesn't stop when I press home button).
So, anyone who knows the solution for these problem, please help me. I am new to Android.
Thank you.
I develop an bluetooth app which will connect to a paired device and send a message, but I have to test connection before. I've tried many options, but nothing works in good way. So could you send me any example of code which can do it? I made an thread, but I can't get an good state of connection to build an "if" function. Here is the code:
package com.example.szukacz;
import java.lang.reflect.Method;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
LinearLayout sparowaneUrzadzenia;
public void lokalizowanie() {
Intent intencja = new Intent(this, Lokalizator.class);
startActivity(intencja);
}
public void parowanie(View v) {
Intent intencja = new Intent(this, Parowanie.class);
startActivity(intencja);
}
boolean isRunning;
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
String status = (String)msg.obj;
if(status == "polaczony") {
alarm();
showToast("prawda, zwraca" + status);
} else {
showToast("wykonanie x, zwraca: " + status);
};
}
};
public void alarm() {
showToast("Alarm!!!");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sparowaneUrzadzenia = (LinearLayout) findViewById(R.id.listaUrzadzenGlowna);
pokazSparowane();
}
public void onStop() {
super.onStop();
isRunning = false;
}
public void onStart() {
super.onStart();
Thread testPolaczen = new Thread(new Runnable() {
public void run() {
try {
for(int i = 0; i < 1000000; i++) {
Thread.sleep(5000);
testujPolaczenia();
int stan = 0;
String status = Integer.toString(stan);
Message msg = handler.obtainMessage(1, (String)status);
if(isRunning == true) {
handler.sendMessage(msg);
}
}
} catch (Throwable t) {
// watek stop
}
}
});
isRunning = true;
testPolaczen.start();
}
private void testujPolaczenia() {
}
public void pokazSparowane(){
/*
* Wyświetlanie listy sparowanych urządzeń .
* */
Log.d("INFO","Sparowane dla tego urzÄ…dzenia");
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
Log.d("INFO",device.getName()+" - "+device.getAddress());
// dodawanie urzadzen do listy
Button urzadzenie = new Button(getApplicationContext());
urzadzenie.setText(device.getName());
// urzadzenie.setTextColor(0xffffff); //jak ustawic na czarny kolor napsisów ?
urzadzenie.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
showToast("klik");
lokalizowanie();
}
});
sparowaneUrzadzenia.addView(urzadzenie);
}
} else {
showToast("brak sparowanych urzadzen");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
thanks!
I faced the same problem as I am working on an app which may use TTS while it is running. I think there is no way to check if there is any bluetooth device connected by the BluetoothAdapter class immediately except creating a broadcast receiver and monitor the changes of status of bluetooth.
After scratching my head for a few hours, I found a quite subtle way to solve this problem. I tried, it works pretty well for me.
AudioManager audioManager = (AudioManager) getApplicationContext.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.isBluetoothA2dpOn()) {
//audio is currently being routed to bluetooth -> bluetooth is connected
}
Source: http://developer.android.com/training/managing-audio/audio-output.html
I think it's to late for answer to your question but I think can helps somebody :
If you use Thread you have to create a BroadcastReceiver in your main activity on create :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_digital_metrix_connexion);
BroadcastReceiver bState = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED))
{
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state)
{
case BluetoothAdapter.ACTION_ACL_CONNECTED:
{
//Do something you need here
System.out.println("Connected");
break;
}
default:
System.out.println("Default");
break;
}
}
}
};
}
BluetoothAdapter.STATE_CONNECTED is one state over many, for exemple it's possible to check if device connecting or disconnecting thanks to BluetoothAdapter.ACTION_ACL_DISCONNECTED or BluetoothAdapter.ACTION_ACL_DISCONNECTED_REQUEST .
After, you have to create a filter in your thread class or in you main activity if you don't use thread :
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
This filter check the bluetoothadapter state.
And now you have to register your filter so if you use thread pass context in parameter of your thread like this context.registerReceiver(bState,filter);
or in your main Activity : registerReceiver(bState,filter);
If you have any question don't hesitate to ask me.
Hope I helps you somebody.
I am trying to make a loop in Android app which is triggered by a button click.
After reading tips on making loops/delays on SO ( for example here), I decided to use message handler approach instead of Runnable.
In the code below, toastLoop() is executed and it prints "starting in x" seconds.
However, the message does not seem to be posted with that delay.
Or, the message is posted but the handler does not receive it.
I am a newbie and I am probably making a silly mistake somewhere.
What am I missing in the code below? Or is this code totally stupid?
package com.example.testapp;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class ExecActivity extends FragmentActivity {
static Context context = null;
String LOG_TAG = "FTR";
static boolean test_status = false;
ToastLoop toast_loop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exec);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.exec, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
class ToastLoop {
private final int loop_max_duration = 60; // in seconds
final int TOAST = 1;
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
Log.i(LOG_TAG, "Handler(): msg: " + msg.what);
switch (msg.what) {
case TOAST:
Toast.makeText( ExecActivity.this, "Doing my thing", Toast.LENGTH_SHORT).show();
if ( test_status) { // test is still running
toastLoop();
}
break;
default:
Toast.makeText(ExecActivity.this, "Unhandled", Toast.LENGTH_SHORT).show();
break;
}
}
};
public boolean toastLoop() {
if ( test_status) { // test is still running
long curr_time_milli = System.currentTimeMillis();
long window_position_sec = (long)( ((long)(curr_time_milli/1000))/loop_max_duration); // fraction discarded
long loop_start_time_sec = (window_position_sec + 1 ) * loop_max_duration;
long actual_start_time_milli = loop_start_time_sec * 1000;
Log.i(LOG_TAG, "toastLoop(): starting in " + ((actual_start_time_milli - curr_time_milli)/1000) );
Message msg = handler.obtainMessage( TOAST);
handler.sendMessageAtTime( msg, actual_start_time_milli );
return true;
}
return false;
}
}
public boolean beginTest( View view) {
Log.i(LOG_TAG, "in beginTest()");
test_status = true;
toast_loop = new ToastLoop();
toast_loop.toastLoop();
return true;
}
public boolean endTest( View view) {
Log.i(LOG_TAG, "in endTest()");
test_status = false;
return true;
}
}
I'm using this tutorial . I have login and logout by facebook in MainActivity , and the problem is how can i perform a logout in the next (Home) activity ? For HomeActivity , i create on my own(by create new activity).
Here is the code for home activity
package com.innoark.motivator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import com.facebook.Session;
public class Home extends Activity {
Button button;
MenuItem logout , writepost , send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
if (menu.size() == 0) {
writepost = menu.add(R.string.writepost);
logout = menu.add(R.string.settings);
}
return true;
} else {
menu.clear();
logout = null;
writepost = null;
}
return true;
}
// #Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.equals(logout)) {
return true;
}
if (item.equals(writepost)){
Intent intent = new Intent (this,MainActivity.class);
startActivity(intent);
}
return true;
}
}
Thanks in advance :)
Here is the code for Logout you can check it ..............
signoutbtn = (Button) findViewById(R.id.signout_btn);
signoutbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
SharedPreferences mSharedPreference =getSharedPreferences("SharedData",Context.MODE_PRIVATE);
SharedPreferences.Editor mSaveState = mSharedPreference.edit();
mSaveState.putBoolean("LoginSession", false);
mSaveState.commit();
Intent signout = new Intent(HotTaxiTabActivity.this,Login.class);
signout.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(signout);
}
});
if (item.equals(logout))
{
SharedPreferences mSharedPreference =getSharedPreferences("SharedData",Context.MODE_PRIVATE);
SharedPreferences.Editor mSaveState = mSharedPreference.edit();
mSaveState.putBoolean("LoginSession", false);
mSaveState.commit();
Intent signout = new Intent(HotTaxiTabActivity.this,Login.class);
signout.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(signout);
}
return true;
When i call the purchase screen i can buy the in app product and everything works ok but when the user leave the app and returns to view the product it keeps asking them to buy again,
I need to know how i can make this so when the user purchases the app the store screen doesnt come up anymore and they can access the product, the app is designed so that when the user buys the app they gain access to a new activity with features built in
if anyone could help, I would be most grateful
I used this tutorial which was very helpful in getting me started:[TUT] Simple InApp Billing / Payment By blundell
Here is my code
package com.IrishSign.app;
import java.util.Locale;
import com.IrishSign.app.BillingHelper;
import com.IrishSign.app.R;
import com.IrishSign.app.BillingService;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class IrishSignAppActivity extends Activity implements OnClickListener {
private static final String TAG = "BillingService";
private Context mContext;
private ImageView purchaseableItem;
private Button purchaseButton;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("BillingService", "Starting");
setContentView(R.layout.main);
mContext = this;
Button A = (Button) findViewById(R.id.alphabet);
Button purchaseableItem = (Button) findViewById(R.id.topics);
Button Intro = (Button) findViewById(R.id.intro);
Button G = (Button) findViewById(R.id.about);
purchaseableItem.setOnClickListener(this);
startService(new Intent(mContext, BillingService.class));
BillingHelper.setCompletedHandler(mTransactionHandler);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent1 = new Intent("com.IrishSign.app.alpha");
startActivity(intent1);
}
});
Intro.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent("com.IrishSign.app.Intro");
startActivity(intent1);
}
});
G.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alertDialog = new AlertDialog.Builder(
IrishSignAppActivity.this).setCancelable(false)
.create(); // Reads Update
alertDialog.setTitle("Welcome");
alertDialog.setMessage("-----");//
alertDialog.setButton("Continue",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
Intent intent5 = new Intent(
IrishSignAppActivity.this,
IrishSignAppActivity.class);
}
});
alertDialog.show(); // <-- Shows dialog on screen.
}
});
}
public Handler mTransactionHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
Log.i(TAG, "Transaction complete");
Log.i(TAG, "Transaction status: "
+ BillingHelper.latestPurchase.purchaseState);
Log.i(TAG, "Item purchased is: "
+ BillingHelper.latestPurchase.productId);
if (BillingHelper.latestPurchase.isPurchased()) {
Intent intent = new Intent("com.IrishSign.app.Topics");
startActivity(intent);
}
};
};
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.topics:
if (BillingHelper.isBillingSupported()) {
BillingHelper.requestPurchase(mContext,
"com.blundell.item.passport");
// android.test.purchased or android.test.canceled or
// android.test.refunded or com.blundell.item.passport
} else {
Log.i(TAG, "Can't purchase on this device");
purchaseButton.setEnabled(false); // XXX press button before
// service started will
// disable when it shouldnt
}
break;
default:
// nada
Log.i(TAG, "default. ID: " + v.getId());
break;
}
}
#Override
protected void onPause() {
Log.i(TAG, "onPause())");
super.onPause();
}
#Override
protected void onDestroy() {
BillingHelper.stopService();
super.onDestroy();
}
}
You can check when the application starts by using restoreTransactions. If you have used Managed Products or Subscriptions then only you will get all the details of the user.
For Unmanaged product there is no detail maintained by google.
So call this in your main activity
mBillingService = new BillingService();
mBillingService.setContext(this);
mBillingService.restoreTransactions();
Once you call this in ResponseHandler class there is one method purchaseResponse
purchaseResponse(final Context context,
final PurchaseState purchaseState, final String productId,
final String orderId, final long purchaseTime,
final String developerPayload, final String purchaseToken) {
}
which will return all the details.
You can check purchaseState then after
if (purchaseState == PurchaseState.PURCHASED) {
} else if (purchaseState == PurchaseState.REFUNDED) {
} else if (purchaseState == PurchaseState.CANCELED) {
}