Unable to use Contact Picker - java

I am new to android programming.
I am unable to invoke the contact picker from my activity.
I have referred to many tutorial,formatted my code accordingly but got no result.
The code for MainActivity.java is:-
package com.example.intents;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater mi=getMenuInflater();
mi.inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void invokeForRes(View view)
{
Intent in=new Intent(Intent.ACTION_PICK,Contact.Contacts.CONTENT_URI);
startActivityForResult(in, 1);
}
}
Please help me.......
Thank you in advance.

Use ContactsContract.Contacts.CONTENT_URI, not Contact.Contacts.CONTENT_URI. The latter was the Android 1.x contacts provider, and it was replaced by ContactsContract in Android 2.0, released in late 2009.
/***
Copyright (c) 2008-2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
http://commonsware.com/Android
*/
package com.commonsware.android.rotation.bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
public class RotationBundleDemo extends Activity {
static final int PICK_REQUEST=1337;
Button viewButton=null;
Uri contact=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewButton=(Button)findViewById(R.id.view);
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_REQUEST) {
if (resultCode == RESULT_OK) {
contact=data.getData();
viewButton.setEnabled(true);
}
}
}
public void pickContact(View v) {
Intent i=
new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, PICK_REQUEST);
}
public void viewContact(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, contact));
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (contact != null) {
outState.putParcelable("contact", contact);
}
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
contact=state.getParcelable("contact");
viewButton.setEnabled(contact != null);
}
}
(from this sample project)

Related

Using facebook graph api to retrieve feeds of a page

I am developing an application for a customer, and I have read in app (android platform) posts which he (customer) publishes on a fan page.
I created the method below and when I use the keyword /me runs and got the output to my personal information, but when I use the keyword "729745317037227/feed/" the app closes and returns nothing.
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"729745317037227/feed/",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
Toast.makeText(
getApplicationContext(),
response.getJSONObject()
+ "",
Toast.LENGTH_SHORT).show();
}
}
).executeAsync();
The entire code, I'm putting in the main, for now I'm just trying to make queries and display the results for later use in fragments.
Main class
package com.clubee.marketnow;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.Profile;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class MainActivity extends Activity {
private TextView mainTextView;
private static final int request_code = 5;
public CallbackManager mCallbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
mainTextView = (TextView) findViewById(R.id.lol);
loginButton.setReadPermissions("user_friends");
LoginManager.getInstance().logOut(); //ensures that whenever we start the app we're logged out
LoginManager.getInstance().registerCallback(mCallbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Profile curProfile = Profile.getCurrentProfile();
startNewIntent(curProfile);
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"729745317037227/feed/",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
Toast.makeText(
getApplicationContext(),
response.getJSONObject()
+ "",
Toast.LENGTH_SHORT).show();
}
}
).executeAsync();
}
#Override
public void onCancel() {
// App code
mainTextView.setText("Status: Logging in cancelled");
}
#Override
public void onError(FacebookException exception) {
// App code
mainTextView.setText("Status: EXCEPTION: " + exception.toString());
}
});
}
private void startNewIntent(Profile elUsero){
Intent i = new Intent(this, RetornoFacebook.class);
i.putExtra("firstName", elUsero.getFirstName());
i.putExtra("lastName", elUsero.getLastName());
i.putExtra("picURI", elUsero.getProfilePictureUri(1500,1500).toString());
startActivityForResult(i, request_code);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onResume() {
super.onResume();
// Logs 'install' and 'app activate' App Events.
AppEventsLogger.activateApp(this);
}
#Override
protected void onPause() {
super.onPause();
// Logs 'app deactivate' App Event.
AppEventsLogger.deactivateApp(this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
if ((requestCode == request_code) && (resultCode == RESULT_OK)) {
LoginManager.getInstance().logOut();
mainTextView.setText("Status: Logged Out!");
}
}
}
Can anyone help me understand how I do to return the feed of a fan page?
I am racking my brain for weeks with this and do not know where I am going wrong. I have read a million times the fb documentation, and the max I got it the current result.
PS: When I do research in graph api explorer, using the same key (729745317037227/feed/), the return is a success.
tks a lot
The issue could be because of the type of access token that you are using.
"/me runs and got the output to my personal information": Call to this edge needs a User Access Token with the appropriate permissions.
"when I use the keyword "729745317037227/feed/" the app closes and returns nothing": Call to this edge needs a Page Access Token with the appropriate permissions. You might be using a User Access Token here.
Page Access Token – These access tokens are similar to user access
tokens, except that they provide permission to APIs that read, write
or modify the data belonging to a Facebook Page.

InAppBilling crashes on startup

I'm trying to make an app that I can get a simple implementation of in app purchases. I've been falling this guide http://www.techotopia.com/index.php/Integrating_Google_Play_In-app_Billing_into_an_Android_Application_%E2%80%93_A_Tutorial
but it's fraught with probably outdated information and neglects to include all the names of the packages you need to download from the SDK manager.
The main errors I had from this program were seemingly reference errors like I had not imported a certain library or compatibility files were missing. I managed to resolve all of them in eclipse and there are no errors when I run the code but trying it on a device or in the VM, the app crashes when I try to run it there.
I excluded the key you need for the google play connection for obvious reasons.
package com.a.inappbilling;
import com.a.inappbilling.util.IabHelper;
import com.a.inappbilling.util.IabResult;
import com.a.inappbilling.util.Inventory;
import com.a.inappbilling.util.Purchase;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.content.Intent;
import android.util.Log;
public class InAppBillingActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_in_app_billing);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.in_app_billing, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_in_app_billing,
container, false);
return rootView;
}
}
private Button clickButton;
private Button buyButton;
#Override
protected void onStart() {
super.onStart();
buyButton = (Button)findViewById(R.id.buyButton);
clickButton = (Button)findViewById(R.id.clickButton);
clickButton.setEnabled(false);
String base64EncodedPublicKey =
"key here";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new
IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result)
{
if (!result.isSuccess()) {
Log.d(TAG, "In-app Billing setup failed: " +
result);
} else {
Log.d(TAG, "In-app Billing is set up OK");
}
}
});
}
public void buttonClicked (View view)
{
clickButton.setEnabled(false);
buyButton.setEnabled(true);
}
private static final String TAG = "com.a.inappbilling";
IabHelper mHelper;
}
Add permission to your Manifest file
<uses-permission android:name="com.android.vending.BILLING" />
also add to your Manifest file under the Application node
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
Also just verify you have added InAppBillingActivity.java to your Manifest file.

Android SDK - Changing Properties in Array List

Can anyone please help me out. I'm having problems changing my array list properties. I am more familiar with changing properties via the XML file. Here is my code:
package com.example.examproject;
import java.util.Random;
import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.content.Intent;
public class Menu_Lists extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myAttractions = new String[2];
myAttractions[0] = "Grocery List";
myAttractions[1] = "Go Back";
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, myAttractions));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
switch(position) {
case 0: {
startActivity(new Intent(Menu_Lists.this, MainActivity.class));
break;
}
default: {
startActivity(new Intent(Menu_Lists.this, MainActivity.class));
}
}
}
#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;
}
}
What I would like to do is to change the background color. That's my main goal. I would also like each option to change color whenever the user hovers over a specific option.
Please go easy with the terminologies as well. I am still a beginner with Android SDK.
Thanks in advance.

Can't get android service to work

I'm very new to android. I want to create an application that turns off all sounds at the selected time. I created a service with some code and in Eclipse there's no errors, but when I press the button nothing happens. I can see in Application Manager that my program and the service SilentHours are running. Here's my code:
MainActivity.java
package com.example.silencecontrol;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_NAME = "sending silentHour value to service SilenceHours";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void setSilenceHours(View view) {
EditText editText1 = (EditText)findViewById(R.id.editText1);
TextView textView1 = (TextView)findViewById(R.id.textView1);
if(editText1.length() > 0) {
Intent intent = new Intent(this, SilentHours.class);
String editText1String = editText1.getText().toString();
int silentHour = Integer.parseInt(editText1String);
intent.putExtra(EXTRA_NAME, silentHour);
this.startService(intent);
} else {
textView1.setText("Please enter the silence hour. ");
}
}
}
SilentHours.java
package com.example.silencecontrol;
import java.util.Calendar;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;
public class SilentHours extends Service {
public SilentHours() {
}
protected void onHandleIntent(Intent intent) {
int silentHour = Integer.parseInt(intent.getStringExtra(MainActivity.EXTRA_NAME));
Calendar calendar = Calendar.getInstance();
int currentTime = calendar.get(Calendar.HOUR_OF_DAY);
if(currentTime >= silentHour) {
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
And by the way I can't #Override the onHandleIntent method. If I put the #Override annotation I get error:
The method onHandleIntent(Intent) of type SilentHours must override or implement a supertype method.
Is that annotation necessary?
The method you are looking for is named onStartCommand, not onHandleIntent.
And no, it's not necessary to add this annotation to an overriden method, it's just a very good practice as you can get sure that you respected the signature of the super class.
Let you IDE help you when you code. Simply type "on" then ask for completion to see which method you can override.

No enclosing instance of the type DonationsActivity is accessible

I'm extremely new to all of this coding stuff, and I've gotten pretty far on my own, but I can't seem to figure out this error. All help is appreciated.
"No enclosing instance of the type DonationsActivity is accessible in scope"
package com.ganttbros.shadowui;
import org.donations.DonationsActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class DonateActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donate);
final Button donate = (Button) findViewById(R.id.donatebutton);
donate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.donate, menu);
return true;
}
}
I should tell you that I am attempting to implement this library: https://github.com/dschuermann/android-donations-lib#readme
I have set up the button, but I just need to get it to launch the "DonationsActivity" when pressed.
use
startActivity(new Intent(DonateActivity.this, DonationsActivity.class));
OR
startActivity(new Intent(v.getContext(), DonationsActivity.class));
instead of
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
for starting DonationsActivity Activity from DonateActivity Activity
Change this:
startActivity(new Intent(DonationsActivity.this, DonationsActivity.class));
to:
startActivity(new Intent(DonateActivity.this, DonationsActivity.class));

Categories