找不到符号方法onActivityResult(int,int,Intent)

时间:2015-08-02 17:40:07

标签: java android android-studio in-app-billing onactivityresult

我可以通过以下链接关注应用内结算教程:

Android Studio Google Play In-app Billing Tutorial.

我在一个扩展Base Adapter的Contact Adapter类中实现了这个逻辑。在教程中,它是在一个扩展Activity的类中实现的。

错误来自 onActivityResult()。我读了几个关于这个的问题,我理解这个方法应该写在扩展Activity的类中,但在我的情况下,场景是不同的。

如果没有在MainActivity类中编写 onActivityResult 方法,有没有办法解决这个问题..如果不是我该怎么办?

Heres ContactAdapter.java

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.neirx.myco.smsproject.util.IabHelper;
import com.neirx.myco.smsproject.util.IabResult;
import com.neirx.myco.smsproject.util.Inventory;
import com.neirx.myco.smsproject.util.Purchase;

import java.util.List;


public class ContactAdapter extends BaseAdapter {
    private static final java.lang.String CLASS_NAME = "<ContactAdapter> ";
    Context context;
    List<Contact> objects;
    LayoutInflater lInflater;
    MainActivity activity;

static final String ITEM_SKU = "android.test.purchased";
IabHelper mHelper;

int count = 0;
int get_limit;

private int limit_counter = 0;
private int max_limit = 2;
boolean testbool = true;


public ContactAdapter(Context context, List<Contact> contact) {
    this.context = context;
    objects = contact;
    lInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    activity = (MainActivity) context;
}

@Override
public int getCount() {
    int selectedCount = 0;
    int nonSelectedCount = 0;
    int size = 0;
    for(Contact contact : objects){
        if(contact.isChecked()) selectedCount++;
        else nonSelectedCount++;
    }
    if(activity.isShowSelected()) size += selectedCount;
    if(activity.isShowNonSelected()) size += nonSelectedCount;
    return size;
}


@Override
public Object getItem(int position) {
    return objects.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}


public void buyClick() {
    mHelper.launchPurchaseFlow(activity, ITEM_SKU, 10001, mPurchaseFinishedListener, "mypurchasetoken");
}

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

    }
}

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
    public void onIabPurchaseFinished(IabResult result,Purchase purchase)
    {
        if (result.isFailure()) {
            // Handle error
            Log.d("----FAILURE 1---", "FAIL 1");
            return;
        }
        else if (purchase.getSku().equals(ITEM_SKU)) {
            consumeItem();
            //buyButton.setEnabled(false);
        }
    }
};

public void consumeItem() {
    mHelper.queryInventoryAsync(mReceivedInventoryListener);
}

IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
    public void onQueryInventoryFinished(IabResult result,Inventory inventory) {

        if (result.isFailure()) {
            // Handle failure
            Log.d("----FAILURE 2---", "FAIL 2");
        } else {
            mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),mConsumeFinishedListener);
        }
    }
};

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
            public void onConsumeFinished(Purchase purchase,IabResult result) {

                if (result.isSuccess()) {
                    //clickButton.setEnabled(true);
                    Log.d("----Success ----", "Success");
                } else {
                    // handle error
                    Log.d("----FAILURE 3---", "FAIL 3");
                }
            }
};


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    String base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxW650UixX2dLFECVdOpTh5OpBTqHwsznQAKd/cVcqKhrXROy4+Gj6B7M6wbkhTaloNSzTOf+nw9t1LZZ19Vlr6kcwmtxP+V/HOFwjf/NB69StOONogXtGKDyRrxtVaPM5es3yGy/aP/LXWfTLFQYJvur4AePonuRXz33iufBq5ITDQJ0+0D/o/mGtadJv0ZMsP9LV/qrMqruoqpSdaIiw5TGXdzYlJTuoP3GwS9kRyZKDeG/70KZ28W/ZclVWAdnZ7aCeDURYDV3a4pmGp5/cIvKwbex6Y7KbQYENX5ObSgNoFHLdyPTdkYaeuU9O6pet2TjGUCKr8n4M5KUMZVm8QIDAQAB";
    mHelper = new IabHelper(context, base64EncodedPublicKey);

    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
       public void onIabSetupFinished(IabResult result)
       {
           if (!result.isSuccess()) {
               Log.d("---MY TAG---", "In-app Billing setup failed: " +
                       result);
           } else {
               Log.d("---MY TAG---", "In-app Billing is set up OK");
           }
       }
   });


    SharedPreferences limit_pref = context.getSharedPreferences(Statical.PREF_LIMIT, Context.MODE_PRIVATE);
    get_limit = limit_pref.getInt("LIMIT_KEY",0);
    //Log.d("----- STORED VALUE-----", "Value is: "+get_limit);
    Log.d("----- Max VALUE-----", "Value is: "+max_limit);

    if (view == null) {
        view = lInflater.inflate(R.layout.view_contact, parent, false);//create view file
    }

    if (position == 0) {
        count = 0;
    }

    if (!activity.isShowSelected()) {
        while (objects.get(position + count).isChecked()) {
            count++;
        }
    }
    if (!activity.isShowNonSelected()) {
        while (!objects.get(position + count).isChecked()) {
            count++;
        }
    }

    final Contact contact = objects.get(position + count);
    String contactFirstName = contact.getFirstName();
    String contactSecondName = contact.getSecondName();
    String contactMiddleName = contact.getMiddleName();
    String[] contactNumbers = contact.getNumbers();

    TextView tvName = (TextView) view.findViewById(R.id.tvName);
    TextView tvNumber = (TextView) view.findViewById(R.id.tvNumber);
    final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);

    if(get_limit == max_limit)
    {
        //checkBox.setChecked(contact.isChecked());
        //testbool = false;
        limit_counter = get_limit;
    }
    else if (get_limit == 1)
    {
        limit_counter = 1;
    }


    String fullName;
    switch (MainActivity.sortMode) {
        case Contact.COMPARE_SECOND_NAME:
            fullName = getFullName(contactSecondName, contactFirstName);
            break;
        case Contact.COMPARE_MIDDLE_NAME:
            fullName = getFullName(contactMiddleName, contactFirstName, contactSecondName);
            break;
        default:
            fullName = getFullName(contactFirstName, contactSecondName);
            break;
    }
    tvName.setText(fullName);

    StringBuilder sbNumber = new StringBuilder();
    for (int i = 0; i < contactNumbers.length; i++) {
        sbNumber.append(contactNumbers[i]);
        if (i < contactNumbers.length - 1) {
            sbNumber.append(", ");
        }
    }
    tvNumber.setText(sbNumber);


    if (testbool) {
        //Log.d("Check Boolean Tag 2 ", "testbool:  "+testbool);
        checkBox.setChecked(contact.isChecked());
    }
    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SharedPreferences selected = context.getSharedPreferences(Statical.CONTACTS_SELECTED, Context.MODE_PRIVATE);
            SharedPreferences.Editor editSelected = selected.edit();
            SharedPreferences limit_pref = context.getSharedPreferences(Statical.PREF_LIMIT, Context.MODE_PRIVATE);
            SharedPreferences.Editor edit_limit_pref = limit_pref.edit();

            int k = 0;
            if(limit_counter  == max_limit)
            {
                checkBox.setChecked(false);
                //Toast.makeText(context,"Limit reached !! ", Toast.LENGTH_SHORT).show();
                Log.d("-------LIMIT REACH-----", "value: "+limit_counter);
                edit_limit_pref.putInt("LIMIT_KEY",limit_counter);
                 showAlertDialog();
            }


            if (contact.isChecked() && limit_counter <= max_limit && limit_counter >= 0) {
                limit_counter = limit_counter - 1;
                editSelected.putBoolean(contact.getContactId(), false);
                edit_limit_pref.putInt("LIMIT_KEY",limit_counter);
                contact.setChecked(false);
                Log.d("-------UN CHECKED-----", "Un Checked value: "+limit_counter);
                k = -1;
            }
            else if(!contact.isChecked() && limit_counter <  max_limit){

                limit_counter = limit_counter + 1;
                editSelected.putBoolean(contact.getContactId(), true);
                edit_limit_pref.putInt("LIMIT_KEY",limit_counter);
                contact.setChecked(true);
                Log.d("------- CHECKED -----", "Checked value: "+limit_counter);
                k = 1;
            }


            editSelected.apply();
            edit_limit_pref.apply();
            activity.updateCount(k);
        }
    });

    return view;
}


public void showAlertDialog()
{
    Log.d("-------VALUE-----", "value: "+limit_counter);
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);

    alertDialog.setTitle("Limit Reached!");
    alertDialog.setMessage("Buy Pro Version");
    alertDialog.setIcon(R.drawable.action_bar_logo);

    alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            buyClick();
            Log.d("-------OK PRESSED -----", "value: " + limit_counter);
            dialog.cancel();
        }
    });

    alertDialog.setNegativeButton("Later", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Log.d("----LATER PRESSED -----", "value: " + limit_counter);
            dialog.cancel();
        }
    });

    alertDialog.show();
}


private String getFullName(String first, String second, String third) {
    StringBuilder sbName = new StringBuilder();
    if (!first.isEmpty()) {
        sbName.append(first).append(" ");
    }
    if (!second.isEmpty()) {
        sbName.append(second).append(" ");
    }
    if (!third.isEmpty()) {
        sbName.append(third);
    }
    return sbName.toString();
}

private String getFullName(String first, String second) {
    StringBuilder sbName = new StringBuilder();
    if (!first.isEmpty()) {
        sbName.append(first).append(" ");
    }
    if (!second.isEmpty()) {
        sbName.append(second);
    }
    return sbName.toString();
}

}

1 个答案:

答案 0 :(得分:0)

我建议您创建一个活动来处理此付款,然后您可以恢复正常流程。