How works the method PlusClient getCurrentLocation()? - android

I'm trying recover the location of the class PlusClient, but this method always returns NULL.
In this question the user Lee say:
Have you set your location to be publicly visible? It will only be
returned if you have made it public.
But I tested with the location public and continues return NULL, I try get my location too, and the return is NULL too.
Below my code:
public void onPeopleLoaded(ConnectionResult status, PersonBuffer personBuffer,
String nextPageToken) {
switch (status.getErrorCode()) {
case ConnectionResult.SUCCESS:
mListItems.clear();
try {
int count = personBuffer.getCount();
for (int i = 0; i < count; i++) {
mListItems.add(personBuffer.get(i).getDisplayName()+"\n"+
((personBuffer.get(i).hasCurrentLocation()) ?
personBuffer.get(i).getCurrentLocation() :
"Value NULL"));
}
} finally {
personBuffer.close();
}
mListAdapter.notifyDataSetChanged();
break;
case ConnectionResult.SIGN_IN_REQUIRED:
Log.d(TAG, "Reconectando");
mPlusClient.disconnect();
mPlusClient.connect();
break;
default:
Log.e(TAG, "Error when listing people: " + status);
break;
}
}
I'm doing anything wrong?

Related

Android Google Play Services for turn based game , Not able to receive invitation with default UI

I am new to turn based game development and trying to develop a turn based game. I am using default UI for selecting players
Intent intent = Games.TurnBasedMultiplayer.getSelectOpponentsIntent(getApiClient(), 1, 1, true);
startActivityForResult(intent, RC_SELECT_PLAYERS);
When I select player sand say Play, other player is supposed to get notification. But that player is not receiving invitation / notification.
#Override
public void onSignInSucceeded() {
updateLayouts();
Games.Invitations.registerInvitationListener(getApiClient(), this);
// developer document says that above line is not required
// include or exclude for me it is not making any difference
}
#Override
public void onInvitationReceived(Invitation invitation) {
Toast.makeText(
this,
"An invitation has arrived from "
+ invitation.getInviter().getDisplayName(), Toast.LENGTH_LONG)
.show();
Log.d(TAG, "Invitation arrived from " + invitation.getInviter().getDisplayName());
}
Here is my onActivtityResult
#Override
public void onActivityResult(int request, int response, Intent data) {
super.onActivityResult(request, response, data);
if (request == RC_LOOK_AT_MATCHES) {
// Returning from the 'Select Match' dialog
if (response != Activity.RESULT_OK) {
// user canceled
return;
}
TurnBasedMatch match = data
.getParcelableExtra(com.google.android.gms.games.multiplayer.Multiplayer.EXTRA_TURN_BASED_MATCH);
if (match != null) {
updateMatch(match);
}
Log.d(TAG, "Match = " + match);
}
else if (request == RC_SELECT_PLAYERS) {
if (response != Activity.RESULT_OK) {
// user canceled
return;
}
// get the invitee list
final ArrayList<String> invitees =
data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
Log.d(TAG, "Invitees count = " + invitees.size());
for(int i=0; i<invitees.size(); i++){
Log.d(TAG, invitees.get(i));
}
// get auto-match criteria
Bundle autoMatchCriteria = null;
int minAutoMatchPlayers = data.getIntExtra(
Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
int maxAutoMatchPlayers
= data.getIntExtra(
Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
if (minAutoMatchPlayers > 0) {
autoMatchCriteria
= RoomConfig.createAutoMatchCriteria(
minAutoMatchPlayers, maxAutoMatchPlayers, 0);
Log.d(TAG, "minAutoMatchPlayers > 0");
} else {
autoMatchCriteria = null;
Log.d(TAG, "minAutoMatchPlayers <= 0");
}
TurnBasedMatchConfig tbmc = TurnBasedMatchConfig.builder()
.addInvitedPlayers(invitees)
.setAutoMatchCriteria(autoMatchCriteria).build();
// kick the match off
Games.TurnBasedMultiplayer
.createMatch(getApiClient(), tbmc)
.setResultCallback(new ResultCallback<TurnBasedMultiplayer.InitiateMatchResult>() {
#Override
public void onResult(TurnBasedMultiplayer.InitiateMatchResult result) {
processResult(result);
}
});
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
showSpinner();
}
}
Can Anyone tell me where I am going wrong?
The notification happens after the first player (who created the match) takes his turn by calling Games.TurnBasedMultiplayer.takeTurn() and not right away after the match is created.
Can also try this. If there is no invitations (even when your app is closed), the account-lvl restrictions can block them.

How to do google inapp purchase in an adapter class

I am developing an app which has google in app purchase. There is a button buy now and after clicking the button I have to call inapp purchase but here is the problem I am facing, the buy now button is in an adapter class hence how can I do inapp purchase in an adapter class
here is my code
public void onClick(View v) {
switch (v.getId()) {
case R.id.loadmore_btn:
// call a url with ofset & limit with Thread
if (getbookItems.getContentName() == "LoadMore") {
booksItemsInfo.remove(booksItemsInfo.size() - 1);
}
if (UIAndDataLoader.offset < bookcategoryItem.getCount()) {
if (UIAndDataLoader.offset < DBTotalContentCount) {
UIAndDataLoader.offset = UIAndDataLoader.offset + 10;
UIAndDataLoader.loadFlag = 0;
myActivity.Tostart();
} else {
myActivity.URLConfig = MagURLConfig.bURL
+ MagURLConfig.uMAILIDNAME
+ _Settings.getString("setEmail-ID", null)
+ MagURLConfig.uPASSWORD
+ _Settings.getString("setPassword", null)
+ MagURLConfig.CATEGORYID
+ bookcategoryItem.getCatId() + MagURLConfig.OFFSET
+ DBTotalContentCount + MagURLConfig.LIMIT;
UIAndDataLoader.bookcountlimit = 1;
myActivity.toStartRefresh(true);
}
}
break;
case R.id.btn_buynow:
// System.out.println("this is buy btn------------->");
BookDataLoader.ActionButtonOnclick(btn_txt, action_btn,
getbookItems, "");
break;
case R.id.preview:
BookDataLoader.ActionButtonOnclick(btn_txt, action_btn,
getbookItems, "Preview");
break;
}
}
}
You can declare the adapter class in the same activity.thats why you can user mHelper object.
I done the same functionality in one of application for in puchase item.
like:-
holder.relative_layout_btn_buy.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if(isOnline())
mHelper.launchPurchaseFlow(OnlineStoreList.this,"android.test.purchased",RC_REQUEST,mPurchaseFinishedListener, "");
else
{
NetworkAlert();
}
} catch (Exception e) {
// Toast.makeText(getApplicationContext(),"Please wait...Try after some time!! " ,1).show();
Log.e("Exception", "===>" + e.toString());
complain(e.getMessage());
}
}
});
I hope with will help.Thanks!!

Android: How the View draw the component?

Android question:
When I set text to a view, when the view will update the UI?
Here is my case 1:
for(int i=0;i< 2000; i++){
aTextView.setText("a"+i);
}
aTextView is a MyTextView, that extends from TextView. I overwirte the onDraw as:
public static int counterP = 0;
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
counterP++;
Log.d("MYButton", "onDraw: " + counterP);
}
From the log, I can see, the printed counterP is not consequent. In the loop, the onDraw method is called only 2-4 times.
I did another test, case 2:
boolean notInit = true;
List<String> cmdList = null;
long stSelfRefresh = 0;
String contentStr;
TextView selfTv;
public void onSelfRefreshTv(View v) {
if (cmdList == null || cmdList.isEmpty()) {
Log.e(TAG, "empty now, reset");
notInit = true;
}
if (notInit) {
cmdList = new ArrayList<String>();
for (int i = 0; i < 2000; i++) {
cmdList.add("a" + i);
}
stSelfRefresh = SystemClock.uptimeMillis();
notInit = false;
selfTv = (MyTextView) findViewById(R.id.mytv);
}
contentStr = cmdList.remove(0);
Log.d(TAG, "contentStr = " + contentStr);
selfTv.setText(contentStr);
if (!cmdList.isEmpty()) {
if (!mHandler.sendEmptyMessage(99)) {
Log.e(TAG, "SKIP my self");
}
} else {
Log.d(TAG, "Cost time: " + (SystemClock.uptimeMillis() - stSelfRefresh));
}
}
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 99:
onSelfRefreshTv(null);
break;
default:
break;
}
}
};
The result of case 2 is the counterP is printed out consequent. 1-2000.
I don't know why the textView is updated for each time?
Do you have any idea for this?
Thanks in advance.
******add case 3***********
for(int i=0;i< 2000;i++){
contentStr = cmdList.remove(0);
mHandler.sendEmptyMessage(100);
}
private Handler mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 100:
selfTv.setText(contentStr);
break;
default:
break;
}
}
};
******end of case 3**********
The test result for case 3 is similar with case 1.
It looks like you are running your code on the UI Thread.
This means: as long as your code is running, the drawing code can not run. The onDraw() is called after your loop finished counting and only the last value is actually drawn on the display.
It's as simple as that.
If you want something count up on your display, you can use a AsyncTask to move the counting part into a non-UI Thread:
new AsyncTask<Void,Integer, Void>() {
#Override
protected Void doInBackground(Void... voids) {
for (int i =0; i<2000; i++) {
publishProgress(i);
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
aTextView.setText("a"+values[0]);
}
};
Your second case is like this:
You prepare the list with all 2000 elements.
Set the first element of the list to the TextView and remove the item from the list.
Send yourself a message to run on the UI Thread as soon as the system thinks it is appropriate.
Let go the control of the UI Thread.
Your code is actually finished and the system calls onDraw().
The message from 4 gets processed and you start again with 2.
You see that the system gets control over the UI Thread in the middle of your loop. That's why it's counting visibly.
Case 3 is different from 2:
You are sending 2000 messages in on loop without letting the system handle it.

Android: IntentService getting Result back to ListFragment

I built a fully-functioning FragmentActivity based on this demo.
Originally (like other activities in my app) the data was being loaded into the FragmentActivity.ListFragment.ListView from SQLite in the UI thread.
I have successfully converted two other activities (not fragment- or list-activities) to a ResultReceiver paradigm that uses an IntentService to get the data from SQLite. The obvious difference in this instance is the fact that the custom ResultReceiver is declared in the ListFragment, not the FragmentActivity:
public EWTRReceiver ArrayListFragmentReceiver;
PROBLEM: The IntentService gets the request, pulls the data but is NOT able to get the data back to the ListFragment: NullPointerException on this line in IntentService.rspPlayers():
r.send(mSignal, resultData);
I'm convinced that I'm missing something simple that will make me feel like an idiot...i just need to be pushed in the right direction.
Following is (1) the call from the ListFragment to the IntenService, (2) the onHandleIntent of the IntentService and (3) the method in the IntentService that calls back to the ListFragment (rspPlayers()).
Thanks!
FragmentActivity.ListFragment.callCtlr():
private void callCtlr(int mSignal){
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_FRGACTPLAYERS, "ArrayListFragment.callCtlr() called with signal: "+mSignal);
}
/** Intent to transmit between processes */
Intent intent = new Intent(getActivity(), CtlPlayers.class);
// declare Source of Signal
intent.putExtra("Source", ArrayListFragmentReceiver);
/** process signal to send to CtlPlayers */
switch (mSignal){
case Constants.S_DB_READPLYRLDR:
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_FRGACTPLAYERS, "ArrayListFragment.callCtlr(S_DB_READPLYRLDR) to CtlPlayers.");
}
// Send CtlAlliances the request for the list of Alliances in current Sector
intent.putExtra("CurrentSector", this.getArguments().getString("FSECTR"));
intent.putExtra("CurrentAlliance", this.getArguments().getString("FANAME"));
intent.putExtra("signal", Constants.S_DB_READPLYRLDR);
break;
case Constants.S_DB_READPLYRREC:
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_FRGACTPLAYERS, "ArrayListFragment.callCtlr(S_DB_READPLYRREC) to CtlPlayers.");
}
// Send CtlAlliances the request for the list of Alliances in current Sector
intent.putExtra("CurrentSector", this.getArguments().getString("FSECTR"));
intent.putExtra("CurrentAlliance", this.getArguments().getString("FANAME"));
intent.putExtra("signal", Constants.S_DB_READPLYRREC);
break;
default:
//do nothing
break;
}
getActivity().startService(intent);
}
IntentService.onHandleIntent():
protected void onHandleIntent(Intent intentREQ) {
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(Intent intent) called.");
}
/** get the receiver and bundle from the incoming Intent */
ResultReceiver rInComing = intentREQ.getParcelableExtra("Source");
/** get the bundle from the incoming Intent */
extras = intentREQ.getExtras();
/** variable to convey results to ActDlgAddPlyr */
boolean mAnswer = false;
/** tracks ActSector's context */
Context mCtx = getApplicationContext();
int mSignal = intentREQ.getIntExtra("signal", Constants.R_NO_SIGNAL);
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_CTLPLAYERS, "Signal Received = " + mSignal);
}
switch (mSignal) {
case Constants.R_NO_SIGNAL :
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): NO READABLE SIGNAL WAS RECEIVED!");
}
break;
case Constants.S_DB_ADDPLYR :
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_ADDPLYR received.");
}
synchronized (this) {
// Get the New Sector Name from the bundle
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_ADDPLYR received with New Player.");
}
mAnswer = addNewPlayer(mCtx, extras);
rspPlayers(Constants.R_DB_ADDPLYR, mAnswer, rInComing);
}
break;
case Constants.S_DB_READPLYRLDR :
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_READPLYRLDR received.");
}
synchronized (this) {
// Get the New Sector Name from the bundle
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_READPLYRLDR received.");
}
mAnswer = readPlyrLdr(mCtx, extras);
rspPlayers(Constants.R_DB_READPLYRLDR, mAnswer, rInComing);
}
break;
case Constants.S_DB_READPLYRREC :
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_READPLYRREC received.");
}
synchronized (this) {
// Get the New Sector Name from the bundle
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_CTLPLAYERS, "onHandleIntent(): S_DB_READPLYRREC received.");
}
mAnswer = readPlyrRec(mCtx, extras);
rspPlayers(Constants.R_DB_READPLYRREC, mAnswer, rInComing);
}
break;
} // end switch
}
IntentService.rspPlayers() [sends result back to FragmentActivity.ListFragment]:
private void rspPlayers(int mSignal, boolean mCode, ResultReceiver r) {
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_CTLPLAYERS, "respPlayers() called with signal, code: "+mSignal+", "+mCode);
}
Bundle resultData = new Bundle();
switch (mSignal) {
case Constants.R_DB_ADDPLYR :
if (mCode == true) {
resultData.putInt("reqStatus", Constants.R_TRUE_THAT);
} else {
resultData.putInt("reqStatus", Constants.R_NO_WAY);
}
break;
case Constants.R_DB_READPLYRLDR :
if (mCode == true) {
resultData.putInt("reqStatus", Constants.R_TRUE_THAT);
resultData.putInt("NumLdrRows", NumLdrRows);
resultData.putSerializable("LPlyrList", LPlyrArray);
} else {
resultData.putInt("reqStatus", Constants.R_NO_WAY);
}
break;
case Constants.R_DB_READPLYRREC :
if (mCode == true) {
resultData.putInt("reqStatus", Constants.R_TRUE_THAT);
resultData.putInt("NumRecRows", NumRecRows);
resultData.putSerializable("RPlyrList", RPlyrArray);
} else {
resultData.putInt("reqStatus", Constants.R_NO_WAY);
}
break;
default :
// do nothing
break;
}
try {
r.send(mSignal, resultData);
} catch (java.lang.Exception e) {
Log.e(Constants.TAG_CTLPLAYERS, "rspPlayers(): Exception message: "+e.getMessage());
Log.e(Constants.TAG_CTLPLAYERS, "rspPlayers(): Exception cause: "+e.getCause());
e.printStackTrace();
}
}
I think ArrayListFragmentReceiver is null.
Handler handler = new Handler();
public EWTRReceiver ArrayListFragmentReceiver = new ResultReceiver(handler)
The handler is needed to make sure the response comes back in the original thread. This would be the next crash otherwise.
Check this tutorial for a sample implementation:
http://ramdroid.github.io/androiddev/ResultReceiverDemo.html

Android Getting the GPSStatus always return nothing, why?

my code is:
android.location.GpsStatus.Listener gpsstatusListenerGps = new android.location.GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
GpsStatus gpsStatus = lm.getGpsStatus(null);
switch (event) {
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
locationResult.gotSatellites(gpsStatus);
Log.e("gpsStatus", "gpsStatus");
lm.removeGpsStatusListener(gpsstatusListenerGps);
for (GpsSatellite sat:lm.getGpsStatus(null).getSatellites()) {
Log.e("STATUS", "gpsx.GpsStatus.Sat.fixed: " + sat.usedInFix());
}
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
locationResult.gotSatellites(gpsStatus);
Log.e("FIX", "gpsx.fixed.");
lm.removeGpsStatusListener(gpsstatusListenerGps);
for (GpsSatellite sat:lm.getGpsStatus(null).getSatellites()) {
Log.e("FIXX", "gpsx.GpsStatus.Sat.fixed: " + sat.usedInFix());
}
break;
}
}
};
...
Log.e("sat.hasNext()",""+sat.hasNext());
while (sat.hasNext()){
GpsSatellite oSat = (GpsSatellite) sat.next();
Log.e("gps",""+oSat.getSnr());
}
sat.hasNext is always false.
i always getting nullstring, so i cant get gpsstatuses, why?

Categories

Resources