user3245170
user3245170

Reputation: 31

How to initialize the parse.com code in Android

I am trying to use the Parse.com sdk and I can't get my Parse codes to initialize. I don't know where to put it and I need help because I can't tell if I need to put it in my main or if I need to put it in somewhere else.

This is a few blocks of code I have put in different classes

I created this with the sample

package com.example.musicdroid;
import com.parse.Parse;
import com.parse.ParseACL; 
import com.parse.ParseUser;
import android.app.Application;

public class ParseApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();

    // Add your initialization code here
    Parse.initialize(this, "XXX", "XXX");

    ParseUser.enableAutomaticUser();
    ParseACL defaultACL = new ParseACL();


    ParseACL.setDefaultACL(defaultACL, true);
}

}

This is where I had it originally

import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseInstallation;
import com.parse.PushService;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Main extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String[] choices = { "Rock", "Metal", "Rap", "My Favorites" };
    setListAdapter(new ArrayAdapter<String>(this, R.layout.main,
            R.id.music, choices));
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    switch (position) {
    case 0:
        try {
            startActivity(new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("spotify:user:warnermusicus:playlist:3DRD3GUpQSjK2rryIRVOPS")));
        } catch (ActivityNotFoundException e) {
            final Intent intent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("https://play.google.com/store/apps/details?id=com.spotify.mobile.android.ui"));
            startActivity(intent);
        }
        break;
    case 1:
        try {
            startActivity(new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("spotify:user:warnermusicus:playlist:6iIDHF5tBqzS6uZjMEzBdQ")));
        } catch (ActivityNotFoundException e) {
            final Intent intent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("https://play.google.com/store/apps/details?id=com.spotify.mobile.android.ui"));
            startActivity(intent);
        }
        break;
    case 2:
        try {
            startActivity(new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("spotify:user:digsterdeutschland:playlist:5pvJLjAhcKCHXGOb7pEbBZ")));
        } catch (ActivityNotFoundException e) {
            final Intent intent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("https://play.google.com/store/apps/details?id=com.spotify.mobile.android.ui"));
            startActivity(intent);
        }
        break;
    case 3:
        try {
            startActivity(new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("spotify:user:digsterdeutschland:playlist:1hYWCuFv5Eav9yIZXHwnpd")));
        } catch (ActivityNotFoundException e) {
            final Intent intent = new Intent(
                    Intent.ACTION_VIEW,
                    Uri.parse("https://play.google.com/store/apps/details?id=com.spotify.mobile.android.ui"));
            startActivity(intent);
        }
        break;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Another place I had it. This is the Main Menu

package com.example.musicdroid;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

 public class MainMenu extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String[] choices = { "Main", "Bios", "Artist Search", "How To Use" };
    setListAdapter(new ArrayAdapter<String>(this, R.layout.menu, R.id.Menu,
            choices));
}

protected void onListItemClick(ListView l, View v, int position, long id) {
    switch (position) {
    case 0:
        startActivity(new Intent(MainMenu.this, Main.class));
        break;
    case 1:
        startActivity(new Intent(MainMenu.this, Bios.class));
        break;
    case 2:
        startActivity(new Intent(MainMenu.this, Search.class));
        break;
    case 3:
        startActivity(new Intent(MainMenu.this, How.class));
        break;
    }
}
}

Manifest Code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.musicdroid"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission
    android:name="com.example.musicdroid.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.musicdroid.permission.C2D_MESSAGE" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/fiery_music_symbol"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo" >
    <activity
        android:name="com.example.musicdroid.Main"
        android:label="@string/app_name" >
    </activity>
    <activity android:name=".MainMenu" >
    </activity>
    <activity android:name=".Bios" >
    </activity>
    <activity android:name=".Search" >
    </activity>
    <activity android:name=".How" >
    </activity>
    <activity android:name=".LoginSignupActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Welcome" >
    </activity>

    <service android:name="com.parse.PushService" />

    <receiver android:name="com.parse.ParseBroadcastReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.USER_PRESENT" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.parse.GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.example.musicdroid" />
        </intent-filter>
    </receiver>
</application>

</manifest>

Upvotes: 2

Views: 6778

Answers (5)

ViratBhavsar
ViratBhavsar

Reputation: 104

import android.app.Application; 
import com.parse.Parse; 

public class MyApp extends Application { 

    @Override public void onCreate() { 
        super.onCreate();

        Parse.initialize(this, ApplicationID, ClientKey);
    }
} 
Then, add the following to your <application> in AndroidManifest.xml:
name=".MyApp"

Upvotes: 0

Dheeraj Bhaskar
Dheeraj Bhaskar

Reputation: 19039

Say you can't have custom app class (Eg: Android Library)

You can use the following procedure

Call initialize like this:

if (ParseCheck.isParseInitialized())
  Parse.initialize(context, PARSE.AB4FUN_APP.APPLICATION_ID, PARSE.AB4FUN_APP.CLIENT_KEY);

Create a utility class like below (note the package)

package com.parse;

public class ParseCheck {
  public static boolean isParseInitialized(){
    return Parse.isInitialized();
  }
}

Upvotes: 0

Leeran7742
Leeran7742

Reputation: 21

You should always place the initialize call in the ACTIVITY's onCreate method, just after the setContentView call. Like this:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_name_here);

    Parse.initialize(this, "ApplicationKey", "ClientKey);
    // Rest of your code follows...
}

Once you have that line, you can then do any Parse transactions.

Upvotes: 0

KP_
KP_

Reputation: 1866

Add Your class extending Application inside Manifest File

<application android:name="your packagename.ParseApplication" 

 android:icon="@drawable/icon"  

 android:label="@string/app_name">

I hope this helps..

Upvotes: 3

marson
marson

Reputation: 953

Just put it to your MainActivity.java to onCreate with all the statements, which you have in ParseApplication's class onCreate. Then you can remove this class (ParseApplication.java) It should work properly.

Hope it helps

Upvotes: 1

Related Questions