How can I write command in onclicklistener() so that I can move to next activity as well as my post status dialog will appear on that new activity. I am using intent for switching to next activity and also using postTowall() method. But these two doesn't perform simultaneously. I am using this method:
public void onClick(View v) {
// TODO Auto-generated method stub
postTowall();
Intent intent= new Intent(Frnd.this,Logout.class);
startActivity(intent);
}
private void postTowall() {
facebook.dialog(this, "feed", new DialogListener() {
#Override
public void onFacebookError(FacebookError e) {
}
#Override
public void onError(DialogError e) {
}
public void onComplete(Bundle values) {
String sh = null;
Bundle params = new Bundle();
params.putString("caption", sh);
}
public void onCancel() {
// TODO Auto-generated method stub
}
You can use thread for posting. Use asynctask and inside that post the message to face book. Or you may just write a simple thread and start it.
This asynctask link may help you. it has usage example also:
http://developer.android.com/reference/android/os/AsyncTask.html
You should set other activity to open post dialog. Send via intent a signal if it should or not...
Related
I know there are several answers to this question, but it isn't working for me, currently my code is:
public class LogoutService extends Service {
public static CountDownTimer timer;
#Override
public void onCreate(){
// TODO Auto-generated method stub
super.onCreate();
timer = new CountDownTimer(1 * 60 * 1000, 1000) {
public void onTick(long millisUntilFinished) {
//Some code
Log.v(TAG, "Service Started");
}
public void onFinish() {
Log.v(TAG, "Call Logout by Service");
// Code for Logout
stopSelf();
}
};
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
and in every activity I have:
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
LogoutService.timer.start();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
LogoutService.timer.cancel();
}
But I get a "Unable to resume activity java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.CountDownTimer android.os.CountDownTimer.start()' on a null object reference" when I try to start any of my activities that contain this code. My application isn't too complicated, it's completely offline, so no web service calls or anything, I just need to go back to my login activity after x minutes. Anyone have any ideas?
Here's the one I used: Auto logout after X minutes, Android and http://androidjug.blogspot.com/2015/10/auto-logout-after-15-minutes-due-to.html
You aren't starting the service, so you aren't allocating the timer. Really you should not be using a static for this.
in my app the main page contains, quite a few images to load on my upload manager activity so it can take a few seconds, depending on how many images there are. i planned on creating a splashscreen to do this loading while displaying an image which is not as bad as the default blank screen with title. i have done this, which should work and does, except the setcontentview() does run but does not display.
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
load l=new load();
l.execute(this);
}
class load extends AsyncTask<Activity, Object, Object>{
#Override
protected Object doInBackground(Activity... a) {
// TODO Auto-generated method stub
Log.i("ss", "splash");
Intent intent = new Intent(a[0], UploadManager.class);
startActivity(intent);
a[0].finish();
return null;
}
}
}
does anybody have any suggestions?
and feel free to ask for details i don't think i have explained it all too well.
edit:
thank you guys for the quick responses.
however i believe the problem was that i wasn't using a splash screen for the correct purpose,
the processes involved in:
Intent intent = new Intent(a[0], UploadManager.class);
startActivity(intent);
a[0].finish();
seem to finish instantly, meaning the images in my onCreate method weren't executing until after the splash screen. what i did instead is changed the loading of my grid into an asynktask, as apposed to just doing my images in there.
i now have it loading fast with the images appearing after a few seconds. i shall be implementing a progress dialog of some sort.
anyone else with a similar problem should prioritize making the loading more efficient as i have.
You are passing Context as this in l.execute(this) and in class load you've passed Activity instance.
You can do it in this way and it works like a charm for me
public class SplashScreen extends Activity{
private static int SLPASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SLPASH_TIME_OUT);
}
}
Override onPostExecute method in class load extends AsyncTask<Activity, Object, Object>{ class, which will run when your doInBackground method finishs image downloading.
In onPostExecute you can open your next activity
like
protected void onPostExecute(Void unused) {
Intent intent= new Intent(this, next.class);
startActivity(intent);
}
I've tried implementing an onClickListener on a CameraFragment, however, it never seems to be called. I am probably missing something quite simple. Does anyone have any ideas?
public class CWACCameraFragment extends CameraFragment implements OnClickListener {
//...
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
takePicture();
Toast.makeText(getActivity(),"click",
Toast.LENGTH_LONG).show();
}
Is there a way to ensure that the onClick event occurs?
In the demo app, I added the following to DemoCameraFragment:
#Override
public void onStart() {
super.onStart();
getView().setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e(getClass().getSimpleName(), "got here");
}
});
}
Log messages showed up just fine. Hence, AFAICT, your approach works, so perhaps there is some bug in how you wired in the click listener.
I have followed this tutorial to launch a url on android through jni call. It runs successfully.
In the same way I want to display a toast message from my cocos2dx layer like this:
public static void openURL(String url) {
Toast.makeText(me,url,Toast.LENGTH_LONG).show();
}
But its crashing with error: Can't create handler with thread. Do you know how can I display it correctly?
Try below code this will definately work for you.
First Create one Runnable interface in your class file like this,
Runnable runnable = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Your url string...",Toast.LENGTH_SHORT).show();
}};
Then create one Handler object and call that runnable interface like below,
Create Handler object like,
Handler handler;
initialize it like,
onCreate(){
.................
handler = new Handler();
.................
}
then call runnable whenever you want like,
handler.post(runnable);
You can't run UI stuffs on a background thread.
You should use an AsyncTask and put that code in the on pre/post execute or if your just displaying a toast you can run it on the UI thread
runOnUiThread(new Runnable() {
}
So this was from 2012.
I guess not a lot people use cocos2d-x.
Ok this how you do this on cocos2d-x.
Edit the AppActivity.java
public class AppActivity extends Cocos2dxActivity
{
private Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.activity = this;
showToast();
}
public void showToast()
{
activity.runOnUiThread(new Runnable()
{
#Override
public void run()
{
// TODO Auto-generated method stub
Toast.makeText(activity, "Hello :D", 10).show();
}
});
}
}
This works very nice in cocos2d-x version 3.x
I test it. Of course JNI just will call the method and this must work.
I wrote this app that in the first screen it has an included Thread on it. So it has I timed it like 7 seconds then it will proceed to the next activity.
The problem is whenever I hit the home button the music will stop and it will go to android homescreen but after my timed is done which is the 7 seconds, the app will reappear and will show the next activity.
I tried putting finish(); in the onpause(); but it's still showing the next activity.
here's the actual code.
public class HelloWorldActivity extends Activity {
MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mp = MediaPlayer.create(this, R.raw.otj);
mp.start();
Thread LogoTimer = new Thread(){
public void run(){
try{
int LogoTimer = 0;
while(LogoTimer < 7000){
sleep(100);
LogoTimer = LogoTimer + 100;
}
startActivity(new Intent("com.example.HelloWorld.CLEARSCREEN"));
} catch (InterruptedException e) {
e.printStackTrace();
}
finally{
finish();
}
}
};
LogoTimer.start();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.release();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mp.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
}
First, that's a really inefficient way to run a timer. Try this way instead:
new Handler().postDelayed(new Runnable() {
public void run() {
// Do some work.
}
}, delayTimeInMs);
Second, your starting a new activity when that timer eventually fires. It doesn't matter that the originating activity is finished. Your startActivity() is running on it's own thread and will execute regardless.
It's possible the postDelayed() method will function like you expect. If not you'll need to have it check when it runs whether it should really start the activity. However, I think the Handler is attached to the default Looper which means it will stop (or rather, the message won't be posted) if the main activity finishes.
The application is still in the background and the thread is not destroyed so it will fire the startActivity.
I would not really setup a splash screen this way, or use a thread unless I wanted it off the UI for some reason, even then there are better options.
For educational purposes to take care of this you need to be able to abort the thread safely in onPause() one way to do so is below
Modifed Thread
Thread LogoTimer = new Thread() {
private volatile boolean abortThread = false;
public void run(){
long stopAt = System.currentTimeMillis() + 7000;
while (!abortThread && stopAt > System.currentTimeMillis())
yield();
if (!abortThread)
startActivity ...
}
public synchronized void stopThread() {
abortThread = true;
}
};