简体   繁体   English

使用Android库项目的付费和免费应用程序-如何有效编码?

[英]Paid and Free App using Android Library Project - How to code effectively?

I am planning to release a paid version of my free android app using the android library project approach. 我打算使用android库项目方法发布免费的android应用程序的付费版本。 http://developer.android.com/tools/projects/index.html http://developer.android.com/tools/projects/index.html

My app has several stand-alone (non-user interface) classes and resources that are easy to reuse between the paid and free version. 我的应用程序具有几个独立的(非用户界面)类和资源,可轻松在付费版本和免费版本之间重用。

My question is about the best way to manage the user interface logic (code in Activity classes). 我的问题是有关管理用户界面逻辑(Activity类中的代码)的最佳方法。 Let's say my free app has one button and my paid app has two buttons in the same activity. 假设我的免费应用程序有一个按钮,而付费应用程序在同一活动中有两个按钮。 Is the best way to achieve this is to have the following setup? 实现此目的的最佳方法是进行以下设置?

Android library project Android库项目

1)Layout with one button 1)一键式布局
2) an Ativity.java file containing logic for when the button is clicked 2)一个Ativity.java文件,其中包含单击按钮时的逻辑

Free app 免费应用

Use layout and source code from the library project 使用库项目中的布局和源代码

Paid app 付费应用

1) A new layout file with two buttons 1)一个带有两个按钮的布局文件
2) A new Activity.java which has the exact same code for handling button1 clicks and new code for handling button2 clicks. 2)一个新的 Activity.java,它具有完全相同的用于处理button1单击的代码和用于处理button2单击的新代码。

This does not seem right because button1's logic in paid app seems to be a wasteful copy ... Is there a better way to do this? 这似乎不对,因为在付费应用程序中button1的逻辑似乎是浪费的副本……是否有更好的方法来做到这一点?

You can make a single project library with all the functionality 您可以使用所有功能创建一个项目库

Just you need a one method that can identify that if application is paid or free 您只需要一种可以确定应用程序是付费还是免费的方法

For that follow the steps 为此,请按照以下步骤操作

1) Create a new application suppose testFree 1)假设testFree创建一个新的应用程序

2) Create a new Application Class as follow in the library project 2)在库项目中创建一个新的应用程序类,如下所示

package com.example.testlib;

import android.app.Application;

public class App extends Application{

private static App mInstance;

public App() {
    mInstance = this;
}

public static App getInstance() {
    return mInstance;
}

public boolean isFree()
{
    return true;
}
}

3) create a new application suppose testPaid 3)假设testPaid创建一个新的应用程序

4) create a new Application class in the testPaid Application as Follow 4)在testPaid应用程序中创建一个新的Application类,如下所示

package com.example.testpaid;

import com.example.testlib.App;

public class AppPaid extends App {

@Override
public boolean isFree() {
    // TODO Auto-generated method stub
    return false;
}

}

5) set Application name on testFree app to Application class that we created on the library class and also set main and launcher activity from library class 5)将testFree应用程序上的应用程序名称设置为我们在库类上创建的应用程序类,并从库类中设置主程序和启动器活动

<application
    android:name="com.example.testlib.App"
   ....
    <activity
        android:name="com.example.testlib.MainActivity"
       android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

6) now set the application name to create application class of testPaid app and also main and launcher activity from library project as follows 6)现在设置应用程序名称以创建testPaid应用程序的应用程序类,以及库项目中的main和launcher活动,如下所示

 <application
    android:name="com.example.testpaid.AppPaid"
    ....
    <activity
        android:name="com.example.testlib.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

7) All you have set now in any class of library project you have a method that will check if you app is free or paid you can check as following way and based on that you can make visible some paid functionality to paid app and some free to free 7)您现在在任何类别的图书馆项目中所设置的所有内容,都有一种方法可以检查您的应用程序是免费还是付费的,您可以按照以下方式进行检查,并在此基础上可以使付费应用程序看到某些付费功能,而某些则免费释放

 if(App.getInstance().isFree())
        {
            Toast.makeText(getActivity(), "Free App", Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getActivity(), "Paid App", Toast.LENGTH_SHORT).show();
        }

Let me know if you still find any problem..... 让我知道您是否仍然有任何问题.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM