PDA

View Full Version : اجرای متد در یک اکتیویتی موجود در یک کلاس



samiasoft
دوشنبه 25 تیر 1397, 17:42 عصر
سلام دوستان

من برای پرداخت درون برنامه ای بازار دستوراتشو طبق مستنداتش داخل اکتیویتی نوشتم و به خوبی هم اجرا میشوند.

اما من میخواستم این دستورات را داخل یک کلاس بزارم و در اکتیویتی اونارو صدا بزنم..اما متاسفانه نتونستم...ممنون میشوم راهنمایی کنید :

public class MainActivity extends AppCompatActivity {

static final String base64EncodedPublicKey = "";
static final String TAG = "payment";
static final String SKU_PREMIUM = "";
boolean mIsPremium = false; // Does the user have the premium upgrade?
static final int RC_REQUEST =1397 ;
IabHelper mHelper;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);




pay_setup();




}




public void button_click(View v){


if (v.getId()==R.id.btn_payment){


check();

}
}






public void pay_setup(){


mHelper = new IabHelper(this, base64EncodedPublicKey);
Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");


if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log.d(TAG, "Problem setting up In-app Billing: " + result);
}
// Hooray, IAB is fully set up!
mHelper.queryInventoryAsync(mGotInventoryListener) ;
}
});
}


public void check(){


if (mHelper != null)mHelper.flagEndAsync();
mHelper.launchPurchaseFlow(this, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener, "payload-string");




}


IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
if (result.isFailure()) {
Log.d(TAG, "Failed to query inventory: " + result);
return;
}
else {
Log.d(TAG, "Query inventory was successful.");
// does the user have the premium upgrade?
mIsPremium = inventory.hasPurchase(SKU_PREMIUM);
// update UI accordingly
Toast.makeText(getBaseContext(),"User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"), Toast.LENGTH_LONG).show();


}
Log.d(TAG, "Initial inventory query finished; enabling main UI.");
}
};


IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {
Log.d(TAG, "Error purchasing: " + result);
return;
}
else if (purchase.getSku().equals(SKU_PREMIUM)) {
// give user access to premium content and update the UI
Toast.makeText(getBaseContext(),"خرید موفق",Toast.LENGTH_SHORT).show();
}
}
};




@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
} else {
Log.d(TAG, "onActivityResult handled by IABUtil.");
}
}


@Override
public void onDestroy() {
super.onDestroy();
if (mHelper != null) mHelper.dispose();
mHelper = null;
}










}

samiasoft
دوشنبه 25 تیر 1397, 22:33 عصر
دوستان اومدم یک کلاس بنام payment_bazarدرست کردم و بدین صورت دستورات را تعریف کردم :


public class payment_bazar {


static final String base64EncodedPublicKey = "";
static final String TAG = "payment";
static final String SKU_PREMIUM = "";
boolean mIsPremium = false; // Does the user have the premium upgrade?
static final int RC_REQUEST =1397 ;
IabHelper mHelper;


Context c;


public payment_bazar(Context c) {
this.c = c;
}


public void pay_setup(){




mHelper = new IabHelper(c, base64EncodedPublicKey);
Log.d(TAG, "Starting setup.");
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
Log.d(TAG, "Setup finished.");




if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log.d(TAG, "Problem setting up In-app Billing: " + result);
}
// Hooray, IAB is fully set up!
mHelper.queryInventoryAsync(mGotInventoryListener) ;
}
});




}




public void check(){


if (mHelper != null)mHelper.flagEndAsync();
mHelper.launchPurchaseFlow((Activity) c, SKU_PREMIUM, RC_REQUEST, mPurchaseFinishedListener, "payload-string");








}




IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
Log.d(TAG, "Query inventory finished.");
if (result.isFailure()) {
Log.d(TAG, "Failed to query inventory: " + result);
return;
}
else {
Log.d(TAG, "Query inventory was successful.");
// does the user have the premium upgrade?
mIsPremium = inventory.hasPurchase(SKU_PREMIUM);
// update UI accordingly
Toast.makeText(c,"User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM"), Toast.LENGTH_LONG).show();




}
Log.d(TAG, "Initial inventory query finished; enabling main UI.");
}
};




IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
if (result.isFailure()) {
Log.d(TAG, "Error purchasing: " + result);
return;
}
else if (purchase.getSku().equals(SKU_PREMIUM)) {
// give user access to premium content and update the UI


Toast.makeText(c,"خرید موفق",Toast.LENGTH_SHORT).show();
}
}
};








}




در اکتیویتی هم بدین صورت از این کلاس استفاده کردم :

public class MainActivity extends AppCompatActivity {

payment_bazar payment_bazar;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);




payment_bazar = new payment_bazar(this);
payment_bazar.pay_setup();


}




public void button_click(View v){


if (v.getId()==R.id.btn_payment){




payment_bazar.check();


}
}












@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (! payment_bazar.mHelper.handleActivityResult(request Code, resultCode, data)) {


super.onActivityResult(requestCode, resultCode, data);
} else {
Log.d(payment_bazar.TAG, "onActivityResult handled by IABUtil.");
Toast.makeText(this,"2",Toast.LENGTH_SHORT).show();
}
}


@Override
public void onDestroy() {
super.onDestroy();
if ( payment_bazar.mHelper != null) payment_bazar.mHelper.dispose();
payment_bazar.mHelper = null;


}




}

حال سوالی که داشتم این هستش که ایا میتوانم متد های onActivityResult و onDestroy را داخل اون کلاس ببرم تا دیگر داخل اکتیویتی نباشند؟ برای اینکار حتما باید به نظرم کلاس مربوطه از Fragment extande کنه درسته ؟
اما من هرکار که کردم نتونستم تعریف کنم...و دستورات درستی اجرا نشد.
ممنون میشوم راهنمایی کنین.