Monday, October 27, 2014

Android developer : How to start activity for result.

Calling a second activity from the main activity by startActivityForResult. When second activity is finish, it will send back result (some data) to main activity.
How main activity get the result ?

From your FirstActivity call the SecondActivity using startActivityForResult() method

 Intent i = new Intent(this, SecondActivity.class);  
 startActivityForResult(i, 1);  

In your SecondActivity set the data which you want to return back to FirstActivity. If you don't want to return back, don't set any.
For example: In secondActivity if you want to send back data:
 Intent returnIntent = new Intent();  
 returnIntent.putExtra("result",result);  
 setResult(RESULT_OK,returnIntent);  
 finish();  

If you don't want to return data:

 Intent returnIntent = new Intent();  
 setResult(RESULT_CANCELED, returnIntent);  
 finish();  

Now in your FirstActivity class override onActivityResult() method. And process intent data by resultCode.
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
   if (requestCode == 1) {  
     if(resultCode == RESULT_OK){  
       String result=data.getStringExtra("result");  
     }  
     if (resultCode == RESULT_CANCELED) {  
       //Write your code if there's no result  
     }  
   }  
 }

reference : http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android

Monday, October 20, 2014

Android developer : how to cancel alarm

if your app create own alarm. how to cancel it ?
it's imposible to get alarm has been created (by name or by id).
this is a simple way to cancel alarm.
To cancel alarm you can use method AlarmManager.cancel(PENDING_INTENT)
Intent in create alarm and cancel alarm must be same.
there is some sample code.

when you create alarm :
 long nextAlarm = new Date().getTime();  
 Intent alarmIntent = new Intent(this, AlarmReceiver.class);  
 alarmIntent.putExtra(Constants.BundleKey.ALARM_TYPE_KEY, Constants.BundleKey.REPEAT_ALARM_VALUE);  
 PendingIntent pendingIntent = PendingIntent.getBroadcast(context,   
                               99, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);  
 AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);  
 alarmManager.set(AlarmManager.RTC_WAKEUP, nextAlarm, pendingIntent);  

when you cancel alarm :
 long nextAlarm = new Date().getTime();   
 Intent alarmIntent = new Intent(this, AlarmReceiver.class);   
 alarmIntent.putExtra(Constants.BundleKey.ALARM_TYPE_KEY, Constants.BundleKey.REPEAT_ALARM_VALUE);   
 PendingIntent pendingIntent = PendingIntent.getBroadcast(context,    
                 99, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);   
 AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);   
 alarmManager.cancel(pendingIntent);      

Intent in create and cancel alarm is same "Intent alarmIntent = new Intent(this, AlarmReceiver.class);"

Happy coding .
cheers :)

Sunday, October 19, 2014

My First Android App

This is my first android app on google play.
It is Football Reminder".  This app help you to see a timer when your favourite team playing, and give you and alarm if you're sleeping.

If you're like please download and give rating. :)

https://play.google.com/store/apps/details?id=com.andri.sasuke.footballreminder

Some screenshoots



 cheers :)