2014-06-20 3 views
2

앱 결제에서 기본 작업을 수행하고 있으며 서비스가 바인딩되지 않습니다. 나는 응용 프로그램을 실행하면InAppBillingService가 바인딩되지 않습니다.

public class MainActivity extends Activity { 

IInAppBillingService mservice; 
ServiceConnection connection; 
String inappid = "android.test.purchased"; // replace this with your in-app 
              // product id 

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

    connection = new ServiceConnection() { 

     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      mservice = null; 

     } 

     @Override 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      mservice = IInAppBillingService.Stub.asInterface(service); 
     } 
    }; 
    getApplicationContext() 
      .bindService(
        new Intent(
          "com.android.vending.billing.InAppBillingService.BIND"), 
        connection, Context.MODE_PRIVATE); 

    Button purchaseBtn = (Button) findViewById(R.id.purchase); 
    purchaseBtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      ArrayList skuList = new ArrayList(); 
      skuList.add(inappid); 
      Bundle querySkus = new Bundle(); 
      querySkus.putStringArrayList("ITEM_ID_LIST", skuList); 
      Bundle skuDetails; 
      try { 
       skuDetails = mservice.getSkuDetails(3, getPackageName(), 
         "inapp", querySkus); 

       int response = skuDetails.getInt("RESPONSE_CODE"); 
       if (response == 0) { 

        ArrayList<String> responseList = skuDetails 
          .getStringArrayList("DETAILS_LIST"); 

        for (String thisResponse : responseList) { 
         JSONObject object = new JSONObject(thisResponse); 
         String sku = object.getString("productId"); 
         String price = object.getString("price"); 
         if (sku.equals(inappid)) { 
          System.out.println("price " + price); 
          Bundle buyIntentBundle = mservice 
            .getBuyIntent(3, getPackageName(), sku, 
              "inapp", 
              "bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ"); 
          PendingIntent pendingIntent = buyIntentBundle 
            .getParcelable("BUY_INTENT"); 
          startIntentSenderForResult(
            pendingIntent.getIntentSender(), 1001, 
            new Intent(), Integer.valueOf(0), 
            Integer.valueOf(0), Integer.valueOf(0)); 
         } 
        } 
       } 
      } catch (RemoteException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (SendIntentException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 1001) { 
     String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA"); 

     if (resultCode == RESULT_OK) { 
      try { 
       JSONObject jo = new JSONObject(purchaseData); 
       String sku = jo.getString(inappid); 
       Toast.makeText(
         MainActivity.this, 
         "You have bought the " + sku 
           + ". Excellent choice,adventurer!", 
         Toast.LENGTH_LONG).show(); 

      } catch (JSONException e) { 
       System.out.println("Failed to parse purchase data."); 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

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

}

에서, bindService의 메소드가 false를 반환하고 mService가 null :

여기 내 주요 활동이다. 로그는 나에게이 메시지를 제공합니다.

Implicit intents with startService are not safe: Intent { act=com.android.vending.billing.InAppBillingService.BIND } android.content.ContextWrapper.bindService:517 com.raico.coinage.MainActivity.onCreate:51 android.app.Activity.performCreate:5231 
+0

isBillingAvailable()을 구현하여 의도 수신기를 사용할 수 있는지 확인할 수 있습니까? 예제를 확인하십시오. http://stackoverflow.com/a/13933785/1237175 –

+0

메소드를 구현했으며 결제가 가능할 때에도 여전히 동일한 문제가 있습니다. –

+0

경고는 알려진 문제점입니다. [이 블로그 게시물] (http://commonsware.com/blog/2014/06/29/dealing-deprecations-bindservice.html)을 참조하십시오. 그러나 API 레벨 20 장치 또는 에뮬레이터에서 실행 중이 아니면 바인딩 실패를 설명하지 않습니다. –

답변

3
는 getApplicationContext()를 호출하기 위해 MainActivity.onCreate()을 변경

을 bindService()를 유사하게이에 :

Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND"); 
serviceIntent.setPackage("com.android.vending"); 
getApplicationContext().bindService(serviceIntent, connection, Context.MODE_PRIVATE); 

이 인 의도를 명시 할 것 더 안전하고 Lollipop에서도 작동해야합니다.

관련 문제