Class Context from other class android - android

I have two Classes (MainActivity & Dialogs)
I am trying to call the Dialogs Class (which as a context) to the MainAcitivity;
I have tried
Dialogs WorkOnDialog = (Dialogs)context;
WorkOnDialog.WorkOnSavedDialog();
This is my MainAcitivty
public class MainActivity extends AppCompatActivity {
Context context = this;
boolean WorkOn = false;
boolean WorkOff = false;
Dialogs WorkOnSaveDialog;
// Dialogs WorkOnSaveDialog = (Dialogs) context;
// Dialogs WorkOffSaveDialog = (Dialogs)context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//Press Work On Button
public void WorkOnClicked(View v) {
//Creates ImageButton var for ActionListener
ImageButton WorkOnClicked = (ImageButton) findViewById(R.id.WorkOn);
WorkOnClicked.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Gets & Sets Date, Filename, Data Content
// Creates Output Stream to save to file
String nowDate = DateFormat.getDateTimeInstance().format(new Date());
String filename = "WorkOnFile";
String string = nowDate + " Work On";
FileOutputStream outputStream;
File file = new File(Environment.getDataDirectory(), filename);
System.out.println("Work On Button CLicked");
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
System.out.println("!Wrote " + string + " !");
System.out.println("Wrote at " + file);
WorkOn = true;
} catch (Exception e) {
e.printStackTrace();
}
Dialogs WorkOnDialog = (Dialogs)context;
WorkOnDialog.WorkOnSavedDialog();
}
});
}
//Press WorkOff Button
public void WorkOffClicked(View v) {
//Creates ImageButton var for ActionListener
ImageButton WorkOffClicked = (ImageButton) findViewById(R.id.WorkOff);
WorkOffClicked.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Gets & Sets Date, Filename, Data Content
// Creates Output Stream to save to file
String nowDate = DateFormat.getDateTimeInstance().format(new Date());
String filename = "WorkOffFile";
String string = nowDate + " Work Off";
FileOutputStream outputStream;
File file = new File(Environment.getDataDirectory(), filename);
System.out.println(" Work Off Button CLicked");
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
System.out.println("!Wrote " + string + " !");
System.out.println("Wrote at " + file);
WorkOn=false;
} catch (Exception e) {
e.printStackTrace();
}
// WorkOffSaveDialog.WorkOffSavedDialog();
}
});
This is my Dialogs class
public class Dialogs extends AppCompatActivity {
public Context context = this;
public void WorkOnSavedDialog() {
new AlertDialog.Builder(context)
.setTitle("Work On File Saved")
.setMessage("[Displaying for Test] Work On File Saved")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
public void WorkOffSavedDialog() {
new AlertDialog.Builder(context)
.setTitle("Work Off File Saved")
.setMessage("[Displaying for Test] Work Off File Saved")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
Can anyone explain to me why I am unable to call this "Dialogs" Class within the main method?
I have searched about calling methods with context, and everything I found did not work out, what exactly is going wrong and why?

You need to make constructor to your Dialog class and pass the MainActivity context to this constructor :
public class Dialogs extends AppCompatActivity {
public Context context;
public Dialogs(Context context) {
this.context = context;
}
public void WorkOnSavedDialog() {
new AlertDialog.Builder(context)
.setTitle("Work On File Saved")
.setMessage("[Displaying for Test] Work On File Saved")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
public void WorkOffSavedDialog() {
new AlertDialog.Builder(context)
.setTitle("Work Off File Saved")
.setMessage("[Displaying for Test] Work Off File Saved")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}

Related

Notify users for new version of Android app

I want to show a notification or dialog (when app opens) if the current installed app is not the Updated Version (available in play store).
How can i do that?
add this dependency to your gradle file..
com.github.rampo.updatechecker:library:2.1.8
And try this code in your Activity..
public static String NEW_VERSION = "1.1.0";
public void checkForAppUpdate () {
try {
if (!((Activity) context).isFinishing()) {
UpdateChecker.setNotice(Notice.NOTIFICATION);
UpdateChecker.setNoticeIcon(R.drawable.your_notification_logo);
String s = "Hello User, New version of this application is now available on play store.";
if (Comparator.isVersionDownloadableNewer((Activity) context, NEW_VERSION)){
SharedPreferences pref = context.getSharedPreferences(UpdateChecker.PREFS_FILENAME, 0);
boolean b = pref.getBoolean(UpdateChecker.DONT_SHOW_AGAIN_PREF_KEY + NEW_VERSION, false);
if (!b) {
displayAlertDialogforPlayStore(context, "Update Available", s);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
And Function displayAlertDialogforPlayStore() is...
public void displayAlertDialogforPlayStore(final Context context, String title,
String message) {
try {
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setIcon(R.drawable.your_notification_logo);
if (title != null) {
alert.setTitle(title);
}
alert.setMessage(message);
alert.setPositiveButton("Update Now", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final String appPackageName = context.getPackageName();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + appPackageName));
context.startActivity(intent);
}
});
alert.setNegativeButton("Later", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert.show();
} catch (Exception e) {
e.printStackTrace();
}
}
This code display both notification and alertDailog for update.
You can use Firebase notifications as described here. You can choose your segment (e.g. app version) as described here.

Use parse.com in android app

I'm new in android developer and I use parse.com in my first android app.
My problem is when the user singup to the app, the user save in the core. but then the app crashes.
This is my code:
save = (Button) findViewById(R.id.saveButton);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!(password.getText().toString().equals(againPassword.getText().toString()))) {
Toast.makeText(getApplicationContext(), "You have mistake in password!", Toast.LENGTH_SHORT).show();
} else {
ParseUser newUser = new ParseUser();
newUser.put("name", firstName.getText().toString());
newUser.put("lastName", lastName.getText().toString());
newUser.setEmail(email.getText().toString());
newUser.setUsername(userName.getText().toString());
newUser.setPassword(password.getText().toString());
newUser.put("rosterArray", rosterArray);
myParse parse = new myParse();
try {
parse.saveUserInParse(newUser);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
});
The code of methed 'saveUserInParse' is
public void saveUserInParse(ParseUser newUser)
{
newUser.signUpInBackground(new SignUpCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
AlertDialog.Builder singUpSucceed = new AlertDialog.Builder(context);
singUpSucceed.setTitle("Sing up succeed!!!");
singUpSucceed.setCancelable(true);
singUpSucceed.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = singUpSucceed.create();
alert11.show();
} else {
String theMessage = e.getMessage();
AlertDialog.Builder singUpSucceed = new AlertDialog.Builder(context);
singUpSucceed.setTitle("Sing up feild!!!");
singUpSucceed.setMessage(theMessage);
singUpSucceed.setCancelable(true);
singUpSucceed.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = singUpSucceed.create();
alert11.show();
}
}
});
}
What did I do wrong? This problam is also in login method.
Thank you.
Here is my demo project for parse Sample Parse, you an use for refrence.

Android writing file issue while setting song as Ringtone

I have successfully written a program to set song as Ringtone, but facing a very small issue
When i do tap on Button first time to set song as ringtone, getting this:
But when I do click on button again (second time), getting this:
So What could be the reason ? Why i never get success in first time...
Here is the complete code of RingtoneActivity.java:
public class RingtoneActivity extends Activity {
private final Context context = this;
private static final String TAG = "Meri Desi Look";
private File sound;
private final File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES);
private MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonRingtone = (Button) findViewById(R.id.btnRingtone);
buttonRingtone.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
ask();
}
});
}
private void ask() {
mediaPlayer = MediaPlayer.create(this, R.raw.meri_desi_look);
mediaPlayer.start();
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Meri Desi Look");
alert.setMessage("You want this sound as your ringtone?");
alert.setCancelable(false);
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
mediaPlayer.stop();
mediaPlayer.reset();
confirmed();
dialog.dismiss();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
mediaPlayer.stop();
mediaPlayer.reset();
dialog.dismiss();
}
});
alert.show();
}
/**
* When the user confirms the popup, this will run, saving the file and
* setting the ringtone utilising the RingtoneManager class.
*/
private void confirmed() {
Boolean success = false;
sound = new File(folder, "meri_desi_look.mp3");
if (!sound.exists()) {
Log.i(TAG, "Writing Meri Desi Look to " + folder.toString());
try {
InputStream in = getResources().openRawResource(R.raw.meri_desi_look);
FileOutputStream out = new FileOutputStream(sound.getPath());
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
} catch (Exception e) {
success = false;
Log.i(TAG, "Desi Look failed to write.");
}
} else {
success = true;
Log.i(TAG, "Meri Desi Look ringtone already there.");
}
if (!success) {
onSetRingtoneError("We couldn't give you a Meri Desi Look ringtone.\n\nThere's an issue writing the file.");
} else {
setRingtone();
}
}
/**
* Physically sets the RingtoneManager preferences.
*/
private void setRingtone() {
try {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, sound.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "Meri Desi Look - performed by Sunny Leone");
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
values.put(MediaStore.Audio.Media.ARTIST, "Meri Desi Look");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(sound.getAbsolutePath());
getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + sound.getAbsolutePath() + "\"",
null);
Uri newUri = getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
onSetRingtoneSuccess();
} catch (Exception e) {
onSetRingtoneError("We couldn't give you a Meri Desi Look ringtone.\n\nThere's an issue setting the file.");
}
}
/**
* Generic helper to show error messages (if ever there are any) :p
*
* #param message
*/
private void onSetRingtoneError(String message) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Meri Desi Look");
alert.setMessage(message);
alert.setCancelable(false);
alert.setPositiveButton("OK. That's too bad.", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
alert.show();
}
private void onSetRingtoneSuccess() {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Meri Desi Look");
alert.setMessage("You've got the ringtone :)\n\nHope you enjoy it!");
alert.setCancelable(false);
alert.setPositiveButton("Sweet!", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
});
alert.show();
}
#Override
protected void onPause() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
}
super.onPause();
}
}
I think you must have figured out long ago where the issue is in the code. But if you didn't,try the following.
private void confirmed() {
...
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
success = true;
}
catch(Exception e) {
success = false;
e.printStackTrace();
}
finally {
n.close();
out.close();
}
...
}

Apply chosen download location as default

I let the user choose his own download location but once chosen, the file still gets downloaded to constant value "HOME".
I think this value needs to be replaced with the chosen download location from user but i have no idea how.
Thank you for helping me.
//choose download location//
public void onClick(final View v) {
switch (v.getId()) {
case R.id.pick_location_dialog_ok: {
if (listener != null) {
final EditText path = (EditText) findViewById(R.id.pick_location_dialog_path);
final String loc = path.getText().toString();
listener.onDownloadLocationChanged(loc);
}
dismiss();
}
break;
case R.id.pick_location_dialog_cancel: {
dismiss();
}
break;
case R.id.pick_location_dialog_choose_path: {
//
}
break;
}
}
//applies onDownloadLocationChanged//
public void onDownloadLocationChanged(final String newLocation) {
final Settings settings = new Settings(this);
final String original = settings.getDownloadsLocation();
if (!newLocation.equals(original)) {
new File(newLocation).mkdirs();
settings.setDownloadsLocation(newLocation);
final SettingsMenuItem item = (SettingsMenuItem) findViewById(R.id.settings_download_location);
item.setValue(newLocation);
final AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle(R.string.app_name);
b.setMessage(R.string.settings_auto_check_updates);
b.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int which) {
new MoveFilesTask(SettingsActivity.this, original, newLocation).execute();
dialog.dismiss();
}
});
b.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int which) {
dialog.dismiss();
}
});
b.create().show();
}
}
}
//
//
//excutes download with choosen path, replace Constants.HOME with chosen download location//
final File parentDir = new File(Constants.HOME, title);
if (!parentDir.exists())
parentDir.mkdirs();
final File file = new File(parentDir, (filename));
notification = new ProgressNotification(DownloaderService.this, file);
super.onStart(intent, startId);
new Thread()
{
public class Constants {
//
public static final File HOME = new File(Environment.getExternalStorageDirectory(),
"homeloc");
//
}
Solved it myself, here is the correct code:
#Override
public void onStart(final Intent intent, final int startId) {
//
final Settings settings = new Settings(this);
final File parentDir = new File(settings.getDownloadsLocation() + "/", title);
//
}

Android cannot bind to service (In App-Billing)

I'm trying to implement in app-billing in my application, but I have a little problem with it. I'm using the example from android developer's site and everytime I start the activity which will connect to the billing service it's showing me a dialog that I cannot connect to server and when I press learn more it's going to a web page which is explaining me to update my android market app, but it's already the latest one. And the other thing, before implementing the code on my application I create a test application where I can connect with the same code and I don't get problems. But in my application I can't do that. Is there any connection with the developer api key, which you can use only in one application or something like that. Because I'm using the same for test and real app.
And here is my code if you can see something which is not really like it should be :
public class StampiiStore extends Activity {
String servername;
int userId;
int storageID;
RPCCommunicator rpc;
String path;
Button envelope1, envelope2, envelope3;
private static final String TAG = "STAMPII";
/**
* The SharedPreferences key for recording whether we initialized the
* database. If false, then we perform a RestoreTransactions request
* to get all the purchases for this user.
*/
private static final String DB_INITIALIZED = "db_initialized";
private mStampiiPurchaseObserver mStampiiPurchaseObserver;
private Handler mHandler;
private BillingService mBillingService;
private TextView mLogTextView;
private Cursor mOwnedItemsCursor;
private PurchaseDatabase mPurchaseDatabase;
private Set<String> mOwnedItems = new HashSet<String>();
/**
* The developer payload that is sent with subsequent
* purchase requests.
*/
private static final int DIALOG_CANNOT_CONNECT_ID = 1;
private static final int DIALOG_BILLING_NOT_SUPPORTED_ID = 2;
#SuppressWarnings("static-access")
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.store);
SystemDatabaseHelper sysDbHelper = new SystemDatabaseHelper(this, null, 1);
sysDbHelper.initialize(this);
ImageView icon = (ImageView) findViewById (R.id.store_img);
final int collId = getIntent().getIntExtra("collection_id",0);
Log.e("collId","collId : "+collId);
// Getting all variables from SharedPreferences to build the right path to images
servername = rpc.getCurrentServerName(this);
Log.d("","Current Server Name : "+servername);
userId = rpc.getUserId(this);
Log.d("","User Id : "+userId);
storageID = rpc.getCurrentStoragePath(this);
Log.d("","storage ID : "+storageID);
//
TextView colltitle = (TextView) findViewById(R.id.collection_title);
TextView sTitle = (TextView) findViewById(R.id.store_collId);
TextView collInfo = (TextView) findViewById(R.id.store_coll_info);
envelope1 = (Button) findViewById(R.id.buyone);
envelope1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBillingService.requestPurchase("android.test.purchased", "");
}
});
envelope2 = (Button) findViewById(R.id.buytwo);
envelope2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBillingService.requestPurchase("android.test.canceled", "");
}
});
envelope3 = (Button) findViewById(R.id.buythree);
envelope3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBillingService.requestPurchase("com.stampii.stampii.envelopesthree", "");
}
});
mHandler = new Handler();
mStampiiPurchaseObserver = new mStampiiPurchaseObserver(mHandler);
mBillingService = new BillingService();
mBillingService.setContext(this);
mPurchaseDatabase = new PurchaseDatabase(this);
setupWidgets();
// Check if billing is supported.
ResponseHandler.register(mStampiiPurchaseObserver);
if (!mBillingService.checkBillingSupported()) {
showDialog(DIALOG_CANNOT_CONNECT_ID);
}
Button back = (Button) findViewById(R.id.store_back_button);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
boolean isLoggedIn = settings.getBoolean("isLoggedIn", false);
ImageButton sync = (ImageButton) findViewById(R.id.sync_store);
if(isLoggedIn){
sync.setVisibility(View.VISIBLE);
} else if(!isLoggedIn){
sync.setVisibility(View.GONE);
}
sync.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(StampiiStore.this, Synchronization.class);
intent.putExtra("process", 2);
startActivity(intent);
}
});
String titleSql = "SELECT title FROM collection_lang WHERE collection_id= " + collId + " AND lang_code='en_US'";
Cursor title = sysDbHelper.executeSQLQuery(titleSql);
if(title.getCount()==0){
title.close();
} else if(title.getCount()>0){
for(title.move(0); title.moveToNext(); title.isAfterLast()){
String collectionTitle = title.getString(title.getColumnIndex("title"));
sTitle.setText(collectionTitle);
if (collectionTitle.length() > 20){
String newTitle = collectionTitle.substring(0, 20);
colltitle.setText(newTitle + "...");
} else {
colltitle.setText(collectionTitle);
}
}
}
title.close();
String infoSql = "SELECT DISTINCT c.total_cards AS cardsCount, " +
" c.cards_per_envelope AS cardsPerEnvelope " +
"FROM collections AS c, collection_lang AS cl " +
"WHERE c.collection_id = cl.collection_id AND c.collection_id=" + collId;
Cursor info = sysDbHelper.executeSQLQuery(infoSql);
if(info.getCount()==0){
info.close();
} else if (info.getCount()>0){
info.moveToFirst();
int cardsCount = info.getInt(info.getColumnIndex("cardsCount"));
int cardsPerEnvelope = info.getInt(info.getColumnIndex("cardsPerEnvelope"));
collInfo.setText(cardsCount+" Stampii\n"+cardsPerEnvelope+" cards per envelope");
}
String sqlite2 = "SELECT media_id FROM collection_media WHERE collection_id="+collId+" AND media_type_id="+3018;
Cursor bg = sysDbHelper.executeSQLQuery(sqlite2);
if (bg.getCount() == 0) {
bg.close();
} else if (bg.getCount() > 0) {
for (bg.move(0); bg.moveToNext(); bg.isAfterLast()) {
int objectId = Integer.parseInt(bg.getString(bg.getColumnIndex("media_id")));
String filename = "mediacollection-"+objectId+".png";
//if(storageID==1){
path = rpc.getPublicPathsInternal(servername, 3018, filename, StampiiStore.this);
/*} else if(storageID==2){
path = rpc.getPublicPathsExternal(servername, 3007, objectId);
}*/
}
}
bg.close();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[3*1024];
Bitmap ops = BitmapFactory.decodeFile(path, options);
BitmapDrawable bitmapDrawable = new BitmapDrawable(ops);
icon.setBackgroundDrawable(bitmapDrawable);
Button promoCode = (Button) findViewById(R.id.promo_code_btn);
promoCode.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SharedPreferences isSelectedCode = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String isSelected = isSelectedCode.getString("isSelected", "");
Log.i("isSelected", "isSelected" + isSelected);
EditText input = new EditText(StampiiStore.this);
input.setText(isSelected);
final int loggedOut = getIntent().getIntExtra("statement", 0);
if(loggedOut==0){
new AlertDialog.Builder(getParent())
.setTitle("Promotional Code")
.setView(input)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog", "Positive");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog","Negative");
dialog.cancel();
}
}).show();
} else if (loggedOut==1){
new AlertDialog.Builder(Collections.parentActivity)
.setTitle("Promotional Code")
.setView(input)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog", "Positive");
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d("AlertDialog","Negative");
dialog.cancel();
}
}).show();
}
}
});
Button savedCodes = (Button) findViewById(R.id.saved_codes_btn);
savedCodes.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent previewMessage = new Intent(getParent(), SavedCodes.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("Saved Codes", previewMessage);
}
});
}
/**
* Sets up the UI.
*/
private void setupWidgets() {
//TODO: If need any changes in the UI!
}
/**
* If the database has not been initialized, we send a
* RESTORE_TRANSACTIONS request to Android Market to get the list of purchased items
* for this user. This happens if the application has just been installed
* or the user wiped data. We do not want to do this on every startup, rather, we want to do
* only when the database needs to be initialized.
*/
private void restoreDatabase() {
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
boolean initialized = prefs.getBoolean(DB_INITIALIZED, false);
if (!initialized) {
mBillingService.restoreTransactions();
Toast.makeText(this, "Restoring Transactions", Toast.LENGTH_LONG).show();
}
}
private void prependLogEntry(CharSequence cs) {
SpannableStringBuilder contents = new SpannableStringBuilder(cs);
contents.append('\n');
contents.append(mLogTextView.getText());
mLogTextView.setText(contents);
}
private void logProductActivity(String product, String activity) {
SpannableStringBuilder contents = new SpannableStringBuilder();
contents.append(Html.fromHtml("<b>" + product + "</b>: "));
contents.append(activity);
prependLogEntry(contents);
}
//PurchaseObserver
private class mStampiiPurchaseObserver extends PurchaseObserver {
public mStampiiPurchaseObserver(Handler handler) {
super(StampiiStore.this, handler);
}
#Override
public void onBillingSupported(boolean supported) {
if (Consts.DEBUG) {
Log.i(TAG, "supported: " + supported);
}
if (supported) {
restoreDatabase();
envelope1.setEnabled(true);
envelope2.setEnabled(true);
envelope3.setEnabled(true);
} else {
showDialog(DIALOG_BILLING_NOT_SUPPORTED_ID);
}
}
#Override
public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
int quantity, long purchaseTime, String developerPayload) {
if (Consts.DEBUG) {
Log.i(TAG, "onPurchaseStateChange() itemId: " + itemId + " " + purchaseState);
}
if (developerPayload == null) {
logProductActivity(itemId, purchaseState.toString());
} else {
logProductActivity(itemId, purchaseState + "\n\t" + developerPayload);
}
if (purchaseState == PurchaseState.PURCHASED) {
mOwnedItems.add(itemId);
}
mOwnedItemsCursor.requery();
}
#Override
public void onRequestPurchaseResponse(RequestPurchase request,
ResponseCode responseCode) {
if (Consts.DEBUG) {
Log.d(TAG, request.mProductId + ": " + responseCode);
}
if (responseCode == ResponseCode.RESULT_OK) {
if (Consts.DEBUG) {
Log.i(TAG, "purchase was successfully sent to server");
}
logProductActivity(request.mProductId, "sending purchase request");
} else if (responseCode == ResponseCode.RESULT_USER_CANCELED) {
if (Consts.DEBUG) {
Log.i(TAG, "user canceled purchase");
}
logProductActivity(request.mProductId, "dismissed purchase dialog");
} else {
if (Consts.DEBUG) {
Log.i(TAG, "purchase failed");
}
logProductActivity(request.mProductId, "request purchase returned " + responseCode);
}
}
#Override
public void onRestoreTransactionsResponse(RestoreTransactions request,
ResponseCode responseCode) {
if (responseCode == ResponseCode.RESULT_OK) {
if (Consts.DEBUG) {
Log.d(TAG, "completed RestoreTransactions request");
}
// Update the shared preferences so that we don't perform
// a RestoreTransactions again.
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean(DB_INITIALIZED, true);
edit.commit();
} else {
if (Consts.DEBUG) {
Log.d(TAG, "RestoreTransactions error: " + responseCode);
}
}
}
}
/**
* Called when this activity becomes visible.
*/
#Override
protected void onStart() {
super.onStart();
ResponseHandler.register(mStampiiPurchaseObserver);
}
/**
* Called when this activity is no longer visible.
*/
#Override
protected void onStop() {
super.onStop();
ResponseHandler.unregister(mStampiiPurchaseObserver);
}
#Override
protected void onDestroy() {
super.onDestroy();
mPurchaseDatabase.close();
mBillingService.unbind();
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_CANNOT_CONNECT_ID:
return createDialog("Server cannot Connect",
"Server cannot connect");
case DIALOG_BILLING_NOT_SUPPORTED_ID:
return createDialog("Billing not supported",
"Billing not supported");
default:
return null;
}
}
/**
* Replaces the language and/or country of the device into the given string.
* The pattern "%lang%" will be replaced by the device's language code and
* the pattern "%region%" will be replaced with the device's country code.
*
* #param str the string to replace the language/country within
* #return a string containing the local language and region codes
*/
private String replaceLanguageAndRegion(String str) {
// Substitute language and or region if present in string
if (str.contains("%lang%") || str.contains("%region%")) {
Locale locale = Locale.getDefault();
str = str.replace("%lang%", locale.getLanguage().toLowerCase());
str = str.replace("%region%", locale.getCountry().toLowerCase());
}
return str;
}
private Dialog createDialog(String titleId, String messageId) {
String helpUrl = replaceLanguageAndRegion(getString(R.string.help_url));
if (Consts.DEBUG) {
Log.i(TAG, helpUrl);
}
final Uri helpUri = Uri.parse(helpUrl);
AlertDialog.Builder builder = null;
final int loggedOut = getIntent().getIntExtra("statement", 0);
if(loggedOut==0){
builder = new AlertDialog.Builder(getParent());
builder.setTitle(titleId)
.setIcon(android.R.drawable.stat_sys_warning)
.setMessage(messageId)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton("Learn More", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, helpUri);
startActivity(intent);
}
});
} else if(loggedOut==1){
builder = new AlertDialog.Builder(Collections.parentActivity);
builder.setTitle(titleId)
.setIcon(android.R.drawable.stat_sys_warning)
.setMessage(messageId)
.setCancelable(false)
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton("Learn More", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, helpUri);
startActivity(intent);
}
});
}
return builder.create();
}
#Override
public void onRestart(){
super.onRestart();
Intent previewMessage = new Intent(StampiiStore.this, StampiiStore.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("StampiiStore", previewMessage);
this.finish();
}
}
And here is how I'm declaring the service in manifest file :
<service android:name="BillingService" />
<receiver android:name="BillingReceiver">
<intent-filter>
<action android:name="com.android.vending.billing.IN_APP_NOTIFY" />
<action android:name="com.android.vending.billing.RESPONSE_CODE" />
<action android:name="com.android.vending.billing.PURCHASE_STATE_CHANGED" />
</intent-filter>
</receiver>
Any suggestions how to connect to the market?
I had the same issue and probably your mistake is the same as mine. I was using a TabHost in my activities and I find out that TabSpec cannot bind to services. So check this :
Using getApplicationContext().bindService instead of just bindService
on your activity solves the problem as it is using the higher level
application context.
Can't you debug through that code? Probably, mBillingService.checkBillingSupported() returns false, caused by failing BillingService.bindToMarketBillingService().
i was facing problem like that on my sony ericssion w8 2.1 android device
what i did are the following :
1) getApplicationContext().bindService (as android-droid said)
2) reset my mobile and downloaded apk for market 2.3.4 version and installed it
3) then open the market app on mobile and clicked accepted and when the app i quitted
4) rerunned the app it was working fine
note: before doing this i tested my app on other device using market version 3.x and it was working so i did all these things ...

Categories

Resources