Android Studio Main Class (Crashlytics, Parse) - android

I am a fairly new to android development, and I don't understant how the main class works on Android Studio.
I'm trying to make my app have the Crashlytics and Parse services but I'm not sure where to put them. Currently I have the code on the OnCreate method in the Login Class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
Parse.initialize(this, "CODE", "CODE");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
But I heard that this code should go on the Application Class... That's because the Application Class is always started and its basically the main Class for the app... I am not sure about this, please correct me if I'm wrong.
If that's the case, how can I access the Application Class and where should I put the code?

Just create a class which should extends the Application , Using this you can initialize the parse installation
public class DemoClass extends Application {
#Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
Parse.initialize(this, "CODE", "CODE");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
Copy this above code

Related

FacebookSdk.sdkInitialize - call it once, or once per activity?

Do I need to put FacebookSdk.sdkInitialize(getApplicationContext()); in every activity? Or just the first activity?
They are asking to initialize it in Application class onCreate() Method.
public class MyApplication extends Application {
// Updated your class body:
#Override
public void onCreate() {
super.onCreate();
// Initialize the SDK before executing any other operations,
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
Link to the original answer

Android - Parse error when initializing activity

When running the activity the first time, Parte initialization (which is inside the onCreate method) goes well:
Parse.enableLocalDatastore(this);
Parse.initialize(this, "...", "...");
Then, If I press the back button and enter again in the activity, I get an error:
java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)`
Which I do not really understand why, because the Parse.enableLocalDatastore(this); is before Parse.initialize(this, "...", "...");.
Well, Ok. Then I tried to retrieve when the enableLocalDatastore has finihed, with Parse.isInitialized() method, but it is private, so I can't use it (as well as some others Parse variables I could use).
After some time, I found that If I call both methods inside a new Thread it works.
I'd like to know why the error happens and why It was solved with the Thread. Also, is there any better way to do it?
Follows the code (trimmed for the important parts):
public class RegisterActivity extends Activity {
Button linkParse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
linkParse = (Button)findViewById(R.id.linkparse);
linkParse.setOnClickListener(new LinkParse());
linkParse.setClickable(false);
try {
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
Parse.initialize(this, "...", "...");
} catch(Exception e){
Toast.makeText(RegisterActivity.this, "Parse not started.", Toast.LENGTH_SHORT).show();
linkParse.setClickable(true);
}
}
class LinkParse implements View.OnClickListener{
#Override
public void onClick(View v) {
Thread thread = new Thread(new StartParse());
thread.start();
}
}
class StartParse implements Runnable{
#Override
public void run() {
try {
// Enable Local Datastore.
Parse.enableLocalDatastore(RegisterActivity.this);
Parse.initialize(RegisterActivity.this, "...", "...");
} catch(Exception e){
}
}
}
}
You should invoke these two lines of codes from the application class not from the activity
Parse.initialize(this, "....","....");
Parse.enableLocalDatatore(this);
There is no need to initialized this multiple times and global application state is meant to be in the application class.
Create a class and extend application and initialize your parse code there
public class MyApplication extends Application{
#Override
public void onCreate(){
super.onCreate()
Parse.initialize(this, "....", "....");
Parse.enableLocalDatastore(this)
}
}
After you have created the application class. Add it to your AndroidManifest.xml
<application android:icon="#drawable/icon"
android:label="#stringapp_name"
android:name="MyApplication">

Why i am getting errors while onCreate() method?

i try to implement AppFlood advertising into my project. I do it with this tutorial . I got into the onCreate() method and then i get multiple errors. First i make imports and get warning The import com.appflood.AppFlood is never used. After this i tried to Initialize AppFlood Object and Splash screen. And after that i get synatx errors.
This is my MainActivity:
import com.appflood.AppFlood;
import com.appflood.AppFlood.AFEventDelegate;
import com.appflood.AppFlood.AFRequestDelegate;
public class onCreate();
AppFlood.initialize(this, "Your app key here", "Your secret key here", AppFlood.AD_ALL);
public class MainActivity extends AndroidApplication implements MyRequestHandler {
private GameHelper gamehelper;
private MyGdxGame mygdxgame;
I think you didn't read the document in full concentration. It said "InitializeAppFloodObject in your onCreate() method and before setContentView() is invoked ". Which means in your main activity there is an overridden method.
Example :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// place here
AppFlood.initialize(this, "Your app key here", "Your secret key here", AppFlood.AD_ALL);
setContentView(R.layout.activity_car_detail);
}
Read the rest of the tutorial carefully.
Edited : I pasted full code without error. Extend your class from activity.
public class Test extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// place here
AppFlood.initialize(this, "Your app key here", "Your secret key here", AppFlood.AD_ALL);
setContentView(R.layout.activity_car_detail);
}}

Libgdx/GooglePlayServices: How do I call to MainActivity(that extends BaseGameActivity, located in the Android Project) from the core project?

I've read so many tutorials and Google documentation on how to implement Google Play Services into my Game, all in how are implemented solely in the Android project. However, LIBGDX has several other packages, namely the core project, where the core Game engine goes.
What I would I like to ask help for is:
For example, if in my core project, the player finishes the "match" and gets a score, and I would like to relay that score to Google Play Services, how do I call to the MainActivity to handle the "score"?
Let's say you have a "desktop" and "android" version of your game. Create an interface in your "core" project that handles all these Google Play Services events. For instance:
public interface GameEventListener {
// Called when a score is to be submitted
pubic void submitScore(int score);
}
Your game instantiation should now receive an implementation of this interface:
public class MyGame extends Game {
public MyGame(GameEventListener listener) {
// Keep the reference to this listener, you'll be calling it from your game
}
}
Then, you have to make your DesktopLauncher and your AndroidLauncher implement this interface and pass themselves as an argument when you create your game:
DesktopLauncher:
public class DesktopLauncher implements GameEventListener {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = Constants.APP_WIDTH;
config.height = Constants.APP_HEIGHT;
new LwjglApplication(new MyGame(new GameEventListener() {
#Override
public void submitScore(int score) {
// Handle the event as you wish for your desktop version
Gdx.app.log("DesktopLauncher", "submitScore");
}
}), config);
}
}
AndroidLauncher (MainActivity):
public class AndroidLauncher extends AndroidApplication implements GameEventListener {
// ...
#Override
public void onCreate(Bundle savedInstanceState) {
// ... example code not intended to work, take only what you need
// Create the layout
RelativeLayout layout = new RelativeLayout(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// Game view. Pass the activity as the argument to your game class
View gameView = initializeForView(new MyGame(this), config);
layout.addView(gameView);
setContentView(layout);
}
#Override
public void submitScore(int score) {
// Submit your score to Google Play Services
gameServicesClient.submitScore(score);
}
}
Now, whenever your "match" is over, send the message to your listener and it will call your MainActivity's implementation of "submitScore" if you are running the Android version of the game:
private void onMatchOver() {
listener.submitScore(match.getFinalScore());
}
Hope it helps.

Parse notification service with listview crashing

I'm just new to android development & I'm facing the problem
I grabbed listview from php/json (mysql output) into android device & its working fine.
For notifications I searched a lot and found GCM is good but bit complicated for me as a started.
I got some easy thing done with Parse Notifications (www.parse.com)
But the problem is, when i put Parse initialize lines in my listview code, the app crashes on pushing notification
here is code for my listview
ListView Code
I also tried to put Parse code in service, but still on receiving notification application crashes
and here is how I put parse lines in listview code
protected void onCreate(Bundle savedInstanceState) {
Parse.initialize(this, "ctMEM5bnp9OBIgiewrsxewq2IGVGt5NEdH7zaD4TCAd", "zX9pmbsadfsdacwezzC18XDXE16O1j0rdlOfCHzbdayS");
PushService.setDefaultPushCallback(this, MainActivity.class);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
accessWebService();
}
Please can someone bing these two lines with my listview in manner they won't crash
Parse.initialize(this, "ctMEM5bnp9OBIgiewrsxewq2IGVGt5NEdH7zaD4TCAd", "zX9pmbsadfsdacwezzC18XDXE16O1j0rdlOfCHzbdayS");
PushService.setDefaultPushCallback(this, MainActivity.class);
You should create separate class Application that extends Application and put it in manifest
import com.parse.Parse;
import com.parse.ParseInstallation;
import com.parse.PushService;
public class Application extends android.app.Application {
public Application() {
}
#Override
public void onCreate() {
super.onCreate();
// Initialize the Parse SDK.
Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");
// Specify an Activity to handle all pushes by default.
PushService.setDefaultPushCallback(this, MainActivity.class);
}
}
Manifiest:
<application android:name="com.parse.tutorials.pushnotifications.Application"
See this complete example: Github

Categories

Resources