This question already has answers here:
How can one detect airplane mode on Android?
(13 answers)
Closed 7 years ago.
I am working on developing an Android app that will detect airplane mode throughout whole application. where to i add this code?
#SuppressWarnings("deprecation")
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isAirplaneModeOn(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
} else {
return Settings.Global.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}
You can add this code where you want to detect whether connection is available.
for example:
#Override
public void onClick(View v) {
boolean isonair = myApp.isAirplaneModeOn(this);
Toast.makeText(this, "IS on AIR? " + isonair, Toast.LENGTH_LONG).show();
}
If you want to have an access for static function use them in Application class:
public class myApp extends Application {
public static function isAirplaneModeOn() {
...
}
}
in any activity use this access: myApp.isAirplaneModeOn()
do not forget update your AndroidManifest.xml:
<application
android:largeHeap="true"
android:name=".myApp" <<<<<<<<<<<<<<<<<<< this is your class name
android:icon="#drawable/somedrawable"
android:label="#string/app_alias"
android:theme="#style/AppTheme" >
Create a Interface like AirplaneModeManager whose Implementation extending broadcast receiver to get notified in the scenario when airplane mode is activated
you can use this code in Implementation to keep a track and use its methods in whole application to get the status !!
Related
#Override
public void getLeaderboardGPGS() {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), getString(R.string.event_score)), 100);
}
else if (!gameHelper.isConnecting()) {
loginGPGS();
}
}
#Override
public void getAchievementsGPGS() {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), 101);
}
else if (!gameHelper.isConnecting()) {
loginGPGS();
}
}
Can anyone explain to me what these methods do? I have them as part of implementing a GoogleApi interface I made in the context of a tutorial. I especially don't understand the 100 / 101 parts, but the whole thing, in general, is quite confusing for me.
PS. I am making a game in LibGDX and this is my first time touching the Google Play API (or I think any API for that matter)
First Method getLeaderboardGPGS show you Leaderboard above your Activity
if you are already Signed in otherwise it start signing process.
Above method definition is from Libgdx wiki but it should be
private final static int REQUEST_CODE_UNUSED = 9002;
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), getString(R.string.leaderboardId)), REQUEST_CODE_UNUSED);
REQUEST_CODE_UNUSED is an arbitrary integer for the request code
getString(R.string.leaderboardId) is LEADERBOARD_ID
taken from Google wiki
Second Method getAchievementsGPGS is used to show a player's achievements, call getAchievementsIntent() to get an Intent to create the default achievements UI.
startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), REQUEST_ACHIEVEMENTS);
where REQUEST_ACHIEVEMENTS is an arbitrary integer used as the request code.
This question already has answers here:
Change app language programmatically in Android
(34 answers)
Closed 6 years ago.
I need to switch between two languages inside the android application itself.
Now, I have string values for two different languages in two strings.xml files in two different folders, one is under Values folder(by default) and another one is under values-fr(for french) which is created when tried Edit translation under language in android studio.But I don't know how to switch between the languages.
It shows the default language (i.e., English) but don't know how to implement a way to switch to other language.
Does any one have easy way of implementing it...?
You can use Android-LocalizationActivity
Here an excerpt from the documentation:
Keep calm and stay easy with multiple language supported in your
android application.
It's basic for android application to be supported multiple languages.
Yeah! It's very easy because android has String Resource. Developer
just had to prepare the text for different languages then android
system will use itself. But frequently problem is "On-time Language
Changing". Because the String Resource was designed to be depending on
current device language. but if we want to change the language by
click some button. It will be difficult to handle it. This problem
will solved because I have created a new library to handle application
language. It called "Localization Activity" library.
Here the example to use it from the documentation:
import android.os.Bundle;
import android.view.View;
import com.akexorcist.localizationactivity.LocalizationActivity;
public class MainActivity extends LocalizationActivity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple);
findViewById(R.id.btn_th).setOnClickListener(this);
findViewById(R.id.btn_en).setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.btn_en) {
setLanguage("en");
} else if (id == R.id.btn_th) {
setLanguage("th");
}
}
}
In the example above, when user click on a button. It will change to English or Thai language.
You can use the locale class to do this .Hope this helps
public class LocaleLanguage {
private static Locale mLocale;
public static void setLocale(Locale locale) {
mLocale = locale;
if(mLocale != null) {
Locale.setDefault(mLocale);
}
}
public static void updateConfig(ContextThemeWrapper wrapper) {
if(mLocale != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Configuration configuration = new Configuration();
configuration.setLocale(mLocale);
wrapper.applyOverrideConfiguration(configuration);
}
}
public static void updateConfig(Application app, Configuration configuration) {
if(mLocale != null && Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
//Wrapping the configuration to avoid Activity endless loop
Configuration config = new Configuration(configuration);
config.locale = mLocale;
Resources res = app.getBaseContext().getResources();
res.updateConfiguration(config, res.getDisplayMetrics());
}
}
}
here is the application class
public class App extends Application {
public void onCreate(){
super.onCreate();
// get user preferred language set locale accordingly new locale(language,country)
LocaleUtils.setLocale(new Locale("iw"));
LocaleUtils.updateConfig(this, getBaseContext().getResources().getConfiguration());
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleUtils.updateConfig(this, newConfig);
}
}
here is the your activity
public class MainActivity extends Activity {
public BaseActivity() {
LocaleUtils.updateConfig(this);
}
}
And for more refer this link for whole tutorial refer this link
I'm new to android development but I give my best.
I want to create an app for scoring a card game for 2 teams playing:
Activity1: Make your bid
Activity2: Evaluate the outcome of the game
Activity3: Show a list with the score of the teams (2 columns)
Start over with Activity1 until someone has won.
So I have activity1 passing data to activity2 (using putExtra with an intent)
Activity2 evaluated the bid and calculates the score of Team1 and Team2.
That data is being passed to activity3.
Here I would like to Show a list with the scores of every round.
But where / how do I save that list data when I move on to activity1 again?
I'm already playing around with a new class which should be extending the application but I do not know how to instantiate that class and how to make it accessible form each activity?
Here is my manifest.xml => extradata as a new class:
<application
android:allowBackup="true"
android:icon="#drawable/notrump"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="extradata">
<activity
This class is set up like this:
public class extradata extends Application{
public static ArrayList<scoredata> scoring = new ArrayList<scoredata>();
#Override
public void onCreate()
{
super.onCreate();
}
And the scoredata class looks like that:
public class scoredata {
private int mScoreteam1;
private int mScoreteam2;
private String mBidteam1;
private String mBidteam2;
public scoredata(int pScoreteam1, int pScoreteam2, String pBidteam1, String pBidteam2){
mScoreteam1 = pScoreteam1;
mScoreteam2 = pScoreteam2;
mBidteam1 = pBidteam1;
mBidteam2 = pBidteam2;
}
public int getScoreteam1(){
return mScoreteam1;
}
public int getScoreteam2(){
return mScoreteam2;
}
public String getBidteam1(){
return mBidteam1;
}
public String getBidteam2(){
return mBidteam2;
}
public void setScoreteam1(int pScoreteam1){
mScoreteam1 = pScoreteam1;
}
public void setScoreteam2(int pScoreteam2){
mScoreteam2 = pScoreteam2;
}
public void setBidteam1(String pBidteam1){
mBidteam1 = pBidteam1;
}
public void setBidteam2(String pBidteam2){
mBidteam2 = pBidteam2;
}
So do I now have an arraylist of scoredata created when the application starts?
And if so how do I access the fields or add an item from my activities?
If you just want to pass the new added values from activity2 to activity1 you can use intents.
Call second activity using startactivityforesult(intent,requestcode). After getting the new values pass the result from activity 2 using setresult().
use this link
Update your arraylist(i.e. add new item) in onActivityResult() function inside activity1.
Use adapter.setnotifychanged to make the updates reflect in listview.
You may want to implement a class as a datastore, and make it a singleton so each of your 3 activities can access the data and modify it.
I don't know if it's the best solution for your problem but it's a simple one.
Does someone knows if it is possible to add push notifications(like Amazon Simple Notification Service) in an Android and iOS with RoboVM libGDX projects? And if it is possible, are there any good tutorials or good hints how to implement such things?
I would be happy about every hint how I can implement it.
Hi I know this is an old question but I was struggling to find a solution for this specially for iOS, but I finally found a way. If the explanation below is confusing and you prefer to see an example here is a github repo with a sample project:
Repo GitHub
I only show the code for iOS see the repo for Android.
The idea is simple you need to create a class that handles sending a notification for each platform on each of your projects (Android and iOS) and have it implement an interface called NotificationsHandler.
NotificationsHandler:
public interface NotificationsHandler {
public void showNotification(String title, String text);
}
iOS Adapter:
public class AdapteriOS implements NotificationsHandler {
public AdapteriOS () {
//Registers notifications, it will ask user if ok to receive notifications from this app, if user selects no then no notifications will be received
UIApplication.getSharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.create(UIUserNotificationType.Alert, null));
UIApplication.getSharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.create(UIUserNotificationType.Sound, null));
UIApplication.getSharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.create(UIUserNotificationType.Badge, null));
//Removes notifications indicator in app icon, you can do this in a different way
UIApplication.getSharedApplication().setApplicationIconBadgeNumber(0);
UIApplication.getSharedApplication().cancelAllLocalNotifications();
}
#Override
public void showNotification(final String title, final String text) {
NSOperationQueue.getMainQueue().addOperation(new Runnable() {
#Override
public void run() {
NSDate date = new NSDate();
//5 seconds from now
NSDate secondsMore = date.newDateByAddingTimeInterval(5);
UILocalNotification localNotification = new UILocalNotification();
localNotification.setFireDate(secondsMore);
localNotification.setAlertBody(title);
localNotification.setAlertAction(text);
localNotification.setTimeZone(NSTimeZone.getDefaultTimeZone());
localNotification.setApplicationIconBadgeNumber(UIApplication.getSharedApplication().getApplicationIconBadgeNumber() + 1);
UIApplication.getSharedApplication().scheduleLocalNotification(localNotification);
}
});
}
}
Now by default Libgdx passes your ApplicationListener or Game object to AndroidLauncher and IOSLauncher along with a configuration object. The trick is to pass the class we created earlier to the ApplicationListener so that you can use it inside your Core project. Simple enough:
public class IOSLauncher extends IOSApplication.Delegate {
#Override
protected IOSApplication createApplication() {
IOSApplicationConfiguration config = new IOSApplicationConfiguration();
// This is your ApplicationListener or Game class
// it will be called differently depending on what you
// set up when you created the libgdx project
MainGame game = new MainGame();
// We instantiate the iOS Adapter
AdapteriOS adapter = new AdapteriOS();
// We set the handler, you must create this method in your class
game.setNotificationHandler(adapter);
return new IOSApplication(game, config);
}
public static void main(String[] argv) {
NSAutoreleasePool pool = new NSAutoreleasePool();
UIApplication.main(argv, null, IOSLauncher.class);
pool.close();
}
}
Now that you have a reference to the implementation of NotificationHandler you can simply call it through your Core project.
public class MainGame extends Game {
// This is the notificatino handler
public NotificationHandler notificationHandler;
#Override
public void create () {
// Do whatever you do when your game is created
// ...
}
#Override
public void render () {
super.render();
// This is just an example but you
// can now send notifications in your project
if(condition)
notificationHandler.showNotification("Title", "Content");
}
#Override
public void dispose() {
super.dispose();
}
// This is the method we created to set the notifications handler
public void setNotificationHandler(NotificationHandler handler) {
this.notificationHandler = handler;
}
}
One last thing
If you need to run the Desktop version then you will need to do the same thing for Desktop otherwise you might get errors, it will not do anything on the Desktop, or you can check the platform before calling the method showNotfication. You can clone the repo where I do this:
Repo GitHub
I've never done it myself. But you can use this tutorial to find out how to write Android specific code in your libGDX project. Your Android code could then receive the notifications and trigger a callback in libGDX. I hope this is at least a step in the right direction.
However I' not sure about doing the same for iOS.
I want to receive notifications for onTrimMemory when available (ICS+) and onLowMemory otherwise (> ICS). My questions are the following:
Will the system allow me to define a class that implements an interface that is not in the current SDK as long as I don't instantiate it? For example, defining CallbacksICS implements ComponentCallbacks2 while targeting minSdk 8, for example, but checking the current SDK_INT before choosing what to instantiate and register.
Will onTerminate always get called when the application is ready to be cleaned up by the system? I vaguely remember hearing in the early days that some of the Application life cycle methods aren't guaranteed to be called.
From my Application class:
#SuppressLint("NewApi")
#Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
callbacksICS = new CallbacksICS();
registerComponentCallbacks(callbacksICS);
}
else {
callbacksLegacy = new CallbacksLegacy();
registerComponentCallbacks(callbacksLegacy);
}
}
#SuppressLint("NewApi")
#Override
public void onTerminate() {
super.onTerminate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
unregisterComponentCallbacks(callbacksICS);
}
else {
unregisterComponentCallbacks(callbacksLegacy);
}
}