📜 ⬆️ ⬇️

We write a math reference book for Android, we connect the Begun ad

Hello, dear Habrovchane! You have seen a lot of posts on the development of android. In this post I will try to explain in more detail my little experience in developing for this OS.

Small introduction


After analyzing the market, I realized that it has a lot of games, entertainment and social applications. There are very few really necessary applications, so I decided to make a “pocket” handbook on higher mathematics with convenient navigation through topics, consisting of three sections: mathematical analysis, analytical geometry, probability theory. Next, everything in order.


Application structure


About creation of application of such plan on Habré already wrote in a post We write a cheat sheet on Android . Of course there are some differences. The first and most important of them is that in our application we need to do a little more Activity, the second is to add social capabilities to the application, such as sending a letter to the developer and independently participating in creating the application to anyone who wants to, also add options menu and several dialogs to the application. of windows, well, we will actually connect Begun advertising to it in order to at least somehow justify the cost of acquiring a developer account on the market.

Activity

MainActivity

When you start the application, the user will see the MainActivity screen:
image
It contains three buttons, which, when clicked, switches to MatanActivity, GeomActivity, or TerverActivity, containing ListView elements in their markup files, representing a set of corresponding topics for the desired subject. The detailed MainActivity code is presented below:
')
public class MainActivity extends Activity {
AdView ad ;
// Declaring the ID of the HELP dialog box and the Exit dialog box
static final int EXIT_DIALOG = 0 ;
static final int HELP_DIALOG = 1 ;


// Call the options menu with one button About
Override
public boolean onCreateOptionsMenu ( Menu menu ) {
MenuInflater inflater = getMenuInflater ( ) ;
inflater. inflate ( R. menu . gameoptions , menu ) ;
menu. findItem ( R. id . about_menu_item ) ;
menu. findItem ( R. id . btn_email ) ;
return true ;
}
// processing clicks on the options menu items
// Call the HELP dialog from the options menu when you click on "About the application" and go to EmailActivity when you click on "contact us"
Override
public boolean onOptionsItemSelected ( MenuItem item ) {
switch ( item. getItemId ( ) ) {
case R. id . about_menu_item :
showDialog ( HELP_DIALOG ) ;
return true ;
case R. id . btn_email :
startActivity ( new Intent ( getApplicationContext ( ) , EmailActivity. class ) ) ;
return true ;
default :
return super . onOptionsItemSelected ( item ) ;
}
}

/ ** Called when the activity is first created. * /
Override
protected void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
requestWindowFeature ( Window . FEATURE_NO_TITLE ) ; // hide the title
setContentView ( R. layout . second ) ;

// connect advertising
// Get the view
ad = ( AdView ) findViewById ( R. id . ad ) ;
ad. onDebug = true ;
// Install event listeners
// callback - the listener, through which you can get information
// about events in the library. The following listeners are still available: AdLoaded, AdClickThru
// and AdStopped. To initialize, you must pass the environmentVars parameter,
// which contains information about the registered site in the system
// (BEGUN_PAD_ID, BEGUN_BLOCK_ID).
// To start initialization, call the ad.init method. Then wait
// callback with adLoaded status and only then call the method
// ad.api ("initAd", al);
ad. setOnApiListener ( new Callback ( ) {
Override
public void init ( ) {
Log d ( "BEGUN_AD" , "ad.api | initAd" ) ;
ArrayList < RequestParam > al = new ArrayList < RequestParam > ( ) ;
RequestParam rp = new RequestParam ( ) ;
rp. name = "environmentVars" ;
rp. value = "pad_id: 261629112 | block_id: 261629500" ;
al. add ( rp ) ;
ad. api ( "initAd" , al ) ;
Log d ( "BEGUN_AD" , "CALLBACK params:" + al. toString ( ) ) ;
}
Override
public void callback ( String c ) {
Log d ( "BEGUN_AD" , "CALLBACK API:" + c ) ;
if ( c. equals ( "AdLoaded" ) ) {
Log d ( "BEGUN_AD" , "startAd" + c ) ;
ad. api ( "startAd" ) ;
}
}
} ) ;
Log d ( "BEGUN_AD" , "ad.init" ) ;
ad. init ( ) ;

// Run MatanActivity
Button b = ( Button ) this . findViewById ( R. id . btn_mathan ) ;
b. setOnClickListener ( new OnClickListener ( ) {
public void onClick ( View arg0 ) {
startActivity ( new Intent ( getApplicationContext ( ) , MatanActivity. class ) ) ;
}
} ) ;

// Start GeomActivity
Button d = ( Button ) this . findViewById ( R. id . btn_geom ) ;
d. setOnClickListener ( new OnClickListener ( ) {
public void onClick ( View arg0 ) {
startActivity ( new Intent ( getApplicationContext ( ) , GeomActivity. class ) ) ;
}
} ) ;

// Run TerverActivity

Button c = ( Button ) this . findViewById ( R. id . btn_terver ) ;
c. setOnClickListener ( new OnClickListener ( ) {
public void onClick ( View arg0 ) {
startActivity ( new Intent ( getApplicationContext ( ) , TerverActivity. class ) ) ;
}
} ) ;

}

protected void onRestart ( ) {
// TODO Auto-generated method stub
super . onRestart ( ) ;
Log d ( "BEGUN_AD" , "resumeAd" ) ;
ad. api ( "resumeAd" ) ;
}


// Create dialogs HELP_DIALOG and EXIT_DIALOG
Override
protected Dialog onCreateDialog ( int id )
{
switch ( id )
{
case HELP_DIALOG :
LayoutInflater inflater = ( LayoutInflater ) getSystemService ( Context . LAYOUT_INFLATER_SERVICE ) ;
View layout = inflater. inflate ( R. layout . help ,
( ViewGroup ) findViewById ( R. id . Root ) ) ;
AlertDialog. Builder builder = new AlertDialog. Builder ( this ) ;
builder. setView ( layout )
. setCancelable ( true )
. setTitle ( "About Application" )
. setNeutralButton ( "OK" , new DialogInterface. OnClickListener ( )
{
public void onClick ( DialogInterface dialog, int id )
{
dialog. cancel ( ) ;
}
} ) ;
AlertDialog alert = builder. create ( ) ;
return builder. create ( ) ;


case EXIT_DIALOG :
AlertDialog. Builder builder3 = new AlertDialog. Builder ( this ) ;
builder3. setMessage ( "Sure you want to quit?" )
. setCancelable ( false )
. setPositiveButton ( "Yes" , new DialogInterface. OnClickListener ( ) {
public void onClick ( DialogInterface dialog, int id ) {
SecondActivity. this . finish ( ) ;
}
} )
. setNegativeButton ( "No" , new DialogInterface. OnClickListener ( ) {
public void onClick ( DialogInterface dialog, int id ) {
dialog. cancel ( ) ;
}

} ) ;
AlertDialog alert3 = builder3. create ( ) ;
return builder3. create ( ) ;

default :
return null ;
}

}


// Call the dialogue EXIT_DIALOG when pressing the back key of the smartphone
Override
public void onBackPressed ( ) {
showDialog ( EXIT_DIALOG ) ;
}

}


About Application Dialog Box
image

Application Exit Dialog Box
image

Advertising has been added to the application more out of interest than from commercial considerations, since Obviously, there is not much to earn in Russian applications. Therefore, I plugged into it a purely Russian advertisement of the Begun system, before this much had been written on Habré about the connection of advertisements, but mostly they were foreign “offices”.

Advantages of using runner advertising

1. Convenient, high-quality API.
2. Convenient withdrawal of money on WebMoney.
3. A good support service, consistently answering any of your questions.

Cons of using a runner ad

The only drawback is that the payment is made only for the transitions, but soon they promise to launch the payment system for both clicks and impressions.

How to connect advertising BEGUN

1. Go to the runner's website in the partners section.
2. Click "Become a partner."
3. Fill out the form.
4. Click register.
5. Next, enter your office on the site.
6. In the upper right corner click “Write” (Go to the menu of writing a letter to the support service).
7. Write to the support service that you have an android application and you would like to include Begun ads in it, you will be sent your pad_id and block_id, which you will insert into the code written above instead of <your pad_id> tags <Your block_id>.

Begun advertising integration

1. In the classpath of the project, you need to add a jar - an advertising library available
at appsmobile.begun.ru/begunAdView.jar .
2. It is necessary to add the following block to the layout:

<en.begun.adlib.AdView
android: id = "@ + id / ad"
android: orientation = "vertical"
android: layout_width = "fill_parent"
android: layout_height = "wrap_content"
android: layout_alignParentBottom = "true"
/>


RelativeLayout is recommended as the parent layer.
3. The manifest file needs to add the following permissions:

<uses-permission android: name = "android.permission.INTERNET" />
<uses-permission android: name = "android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android: name = "android.permission.READ_PHONE_STATE" />


4. The library is initialized as follows:

import ru.begun.adlib.AdView ;
import ru.begun.adlib.Callback ;
import ru.begun.adlib.RequestParam ;
AdView ad ;
// Get the view
ad = ( AdView ) findViewById ( R. id . ad ) ;
// Install event listeners
// callback - the listener, through which you can get information
// about events in the library. The following listeners are still available: AdLoaded, AdClickThru
// and AdStopped. To initialize, you must pass the environmentVars parameter,
// which contains information about the registered site in the system
// (BEGUN_PAD_ID, BEGUN_BLOCK_ID).
// To start initialization, call the ad.init method. Then wait
// callback with adLoaded status and only then call the method
// ad.api ("initAd", al);
ad. setOnApiListener ( new Callback ( ) {
Override
public void init ( ) {
ArrayList < RequestParam > al = new ArrayList < RequestParam > ( ) ;
RequestParam rp = new RequestParam ( ) ;
rp. name = "environmentVars" ;
rp. value = "pad_id: <BEGUN_PAD_ID> | block_id: <BEGUN_BLOCK_ID>" ;
al. add ( rp ) ;
ad. api ( "initAd" , al ) ;
}
Override
public void callback ( String c ) {
if ( c. equals ( "AdLoaded" ) ) {
ad. api ( "startAd" ) ;
}
}
} ) ;
ad. init ( ) ;


To restart advertisements after the library has been stopped,
You must call the ad.api method with the parameter resumeAd.

All advertising is connected and working!

main.xml

Markup file MainActivity.

<? xml version = "1.0" encoding = "utf-8" ?>
<RelativeLayout xmlns: android = " schemas.android.com/apk/res/android"
android: orientation = "vertical"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: weightSum = "1"
android: background = "@ drawable / main_1" >

<Button
android: id = "@ + id / btn1"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: background = "@ layout / button_selector_leftcorner" />

<Button android: textStyle = "bold" android: textColor = "# FFC000" android: background = "@ layout / button_selector" android: layout_height = "wrap_content" android: layout_width = "173dp" android: text = "An. Geometry and lin. algebra " android: id = " @ + id / btn_geom " android: layout_above = " @ + id / btn_terver " android: layout_alignLeft = " @ + id / btn_terver " android: layout_marginBottom = " 16dp " > </ Button >
<Button android: textStyle = "bold" android: textColor = "# FFC000" android: background = "@ layout / button_selector" android: layout_height = "wrap_content" android: layout_width = "173dp" android: text = "Mathematical analysis" android : id = "@ + id / btn_mathan" android: layout_above = "@ + id / btn_geom" android: layout_alignLeft = "@ + id / btn_geom" android: layout_marginBottom = "15dp" > </ Button >
<Button android: textStyle = "bold" android: textColor = "# FFC000" android: background = "@ layout / button_selector" android: layout_height = "wrap_content" android: layout_width = "173dp" android: text = "Probability Theory" android : id = "@ + id / btn_terver" android: layout_alignParentBottom = "true" android: layout_centerHorizontal = "true" android: layout_marginBottom = "186dp" > </ Button >
<ImageView android: id = "@ + id / imageView1" android: src = "@ drawable / andro" android: layout_height = "wrap_content" android: layout_width = "wrap_content" android: layout_above = "+ + id / btn_mathan" android: layout_centerHorizontal = "true" > </ ImageView >
<RelativeLayout android: layout_height = "wrap_content" android: layout_width = "fill_parent" >
<ImageView android: id = "@ + id / imageView2" android: layout_height = "wrap_content" android: layout_width = "wrap_content" android: src = "@ drawable / title" android: layout_alignParentTop = "true" android: layout_centerHorizontal = "true " android: layout_marginTop = " 18dp " > </ ImageView >
</ RelativeLayout >
<en.begun.adlib.AdView
android: id = "@ + id / ad"
android: orientation = "vertical"
android: layout_width = "fill_parent"
android: layout_height = "wrap_content"
android: layout_alignParentBottom = "true"
/>

<ImageView
android: id = "@ + id / splashscreen"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: src = "@ drawable / splashscreen" android: background = "# 000" >
</ ImageView >




</ RelativeLayout >


MatanActivity, GeomActivity and TerverActivity


image

After clicking on the items in the list in MatanActivity, GeomActivity or TerverActivity, the transition will be made to the corresponding activity of displaying material on this topic ViewMatanActivity , ViewGeomActivity or ViewTerverActivity

image

The code of these activations is similar to those specified in the post We write a cheat sheet on Android .

The only note in all ViewActivity instead of code:

WebView myWebView = ( WebView ) findViewById ( R. id . Webview ) ;
String summary = "<html> <body>" + text + "</ body> </ html>" ;
myWebView. loadData ( summary, "text / html" , "utf-8" ) ; // upload text to webview


better to write like this :

WebView myWebView = ( WebView ) findViewById ( R. id . Webview ) ;
String summary = "<html> <body>" + text + "</ body> </ html>" ;
myWebView. loadDataWithBaseURL ( "x-data: // base" , summary, "text / html" , "utf-8" , null ) ;


Otherwise, the code will not work correctly on devices with android 3.2. and some others.

EmailActivity

EmailActivity implements sending an email with your wishes and suggestions to developers, and also provides the ability to go to the application page in the market.
image

public class EmailActivity extends Activity {
Button send ;
EditText address, subject, emailtext ;

Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
requestWindowFeature ( Window . FEATURE_NO_TITLE ) ; // hide the title
setContentView ( R. layout . email ) ;

Button b = ( Button ) this . findViewById ( R. id . btn_market ) ;
b. setOnClickListener ( new OnClickListener ( ) {
public void onClick ( View arg0 ) {
Uri address = Uri. parse ( " market.android.com/details?id=com.mathhelper.math" ) ;
Intent surf = new Intent ( Intent. ACTION_VIEW , address ) ;
startActivity ( surf ) ;
}
} ) ;

// Our fields and button
send = ( Button ) findViewById ( R. id . emailsendbutton ) ;
address = ( EditText ) findViewById ( R. id . emailaddress ) ;
// subject = (EditText) findViewById (R.id.emailsubject);
emailtext = ( EditText ) findViewById ( R. id . emailtext ) ;

send. setOnClickListener ( new OnClickListener ( ) {

Override
public void onClick ( View v ) {

final Intent emailIntent = new Intent ( android. content . Intent . ACTION_SEND ) ;

emailIntent. setType ( "plain / text" ) ;
// To
emailIntent. putExtra ( android. content . Intent . EXTRA_EMAIL , new String [ ] { address. getText ( ) . toString ( ) } ) ;

// About what
emailIntent. putExtra ( android. content . Intent . EXTRA_TEXT , emailtext. getText ( ) . toString ( ) ) ;

EmailActivity. this . startActivity ( Intent. createChooser ( emailIntent,
"Sending a letter" ) ) ;
}
} ) ;
}
}


email.xml


EmailActivity markup file

<? xml version = "1.0" encoding = "utf-8" ?>
<RelativeLayout xmlns: android = " schemas.android.com/apk/res/android"
android: orientation = "vertical"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent"
android: weightSum = "1" >

<ScrollView
android: scrollbars = "vertical"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent" >
<LinearLayout android: orientation = "vertical" android: id = "@ + id / linearLayout2" android: background = "@ drawable / metall" android: layout_alignParentTop = "true" android: layout_alignParentLeft = "true" android: layout_marginTop = "40dp " android: layout_width = " fill_parent " android: layout_height = " 197dp " >
<TextView android: layout_width = "wrap_content" android: text = "Write to us:" android: layout_height = "wrap_content" android: id = "@ + id / textView1" android: gravity = "left" android: textSize = " 10pt " android: textStyle = "normal" > </ TextView >
<EditText android: id = "@ + id / emailaddress" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "svdsoftware@gmail.com" > </ EditText >
<EditText android: id = "@ + id / emailtext" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "Your Feedback ..." > </ EditText >
<RelativeLayout android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: id = "@ + id / relativeLayout1" >
<TextView android: id = "@ + id / textView1" android: text = "Send us an email →" android: layout_width = "wrap_content" android: gravity = "left" android: layout_height = "wrap_content" android: textSize = "9pt " android: textStyle = " normal " android: layout_centerVertical = " true " > </ TextView >
<Button android: id = "@ + id / emailsendbutton" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "@ layout / button_selector_email" android: layout_alignParentTop = "true" android: layout_alignParentRight = "true " > </ Button >
</ RelativeLayout >
<RelativeLayout android: id = "@ + id / relativeLayout2" android: layout_height = "wrap_content" android: layout_width = "fill_parent" >
<Button android: id = "@ + id / btn_market" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "@ layout / button_selector_am" android: layout_alignParentTop = "true" android: layout_alignParentRight = "true " > </ Button >
<TextView android: id = "@ + id / textView1" android: text = "Rate on the market →" android: layout_width = "wrap_content" android: gravity = "left" android: layout_height = "wrap_content" android: textSize = "9pt " android: textStyle = " normal " android: layout_centerVertical = " true " > </ TextView >
</ RelativeLayout >
</ LinearLayout >
</ ScrollView >

</ RelativeLayout >


Conclusion


I hope that the post will be useful to novice developers.
The result of all the work can be found here .

Source: https://habr.com/ru/post/131650/


All Articles