Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:android:start [2018/04/29 11:03] smayr [DB Connector] |
swdev:android:start [2018/05/22 14:15] (current) smayr [Resources] |
||
---|---|---|---|
Line 5: | Line 5: | ||
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// | ||
* [[https:// | * [[https:// | ||
Line 127: | Line 128: | ||
if (intentDialNumber.resolveActivity(getPackageManager()) != null) { | if (intentDialNumber.resolveActivity(getPackageManager()) != null) { | ||
startActivity(intentDialNumber); | startActivity(intentDialNumber); | ||
+ | } | ||
+ | |||
+ | // Call contacts to handle contact selection | ||
+ | Intent intentSelectContact = new Intent(Intent.ACTION_DIAL, | ||
+ | if (intentSelectContact.resolveActivity(getPackageManager()) != null) { | ||
+ | startActivity(intentSelectContact); | ||
} | } | ||
Line 166: | Line 173: | ||
intentDataHandler.putExtra(" | intentDataHandler.putExtra(" | ||
intentDataHandler.putExtra(" | intentDataHandler.putExtra(" | ||
- | startActivity(); | + | |
+ | | ||
break; | break; | ||
} | } | ||
Line 176: | Line 184: | ||
<code java> | <code java> | ||
//... | //... | ||
- | public class ActivityTargetSample | + | public class TargetActivity |
{ | { | ||
@Override | @Override | ||
Line 182: | Line 190: | ||
{ | { | ||
super.onCreate(saveInstanceState); | super.onCreate(saveInstanceState); | ||
- | setContentView(R.layout.activity_target_sample); | + | setContentView(R.layout.activity_target); |
| | ||
- | Bundle bundle = getIntent().getExtras(); | + | |
+ | // Get data payload (if available) | ||
+ | // | ||
+ | // Method 1 | ||
+ | | ||
String str = bundle.getString(" | String str = bundle.getString(" | ||
- | | ||
Toast.makeText(this, | Toast.makeText(this, | ||
| | ||
- | // Better yet | + | // Method 2 |
- | | + | |
- | str = getIntent().getExtras().getString(" | + | if (intent.hasExtra(" |
+ | str = intent.getExtras().getString(" | ||
Toast.makeText(this, | Toast.makeText(this, | ||
+ | } | ||
+ | |||
+ | // Method 3 | ||
+ | if (getIntent().hasExtra(" | ||
+ | str = getIntent().getExtras().getString(" | ||
+ | Toast.makeText(this, | ||
+ | } | ||
+ | |||
+ | // Parcelable data | ||
+ | Team objTeam; | ||
+ | if (intent.hasExtra(" | ||
+ | Log.d(" | ||
+ | objTeam = intent.getParcelableExtra(" | ||
+ | Toast.makeText(this, | ||
} | } | ||
} | } | ||
Line 199: | Line 224: | ||
</ | </ | ||
+ | A parcelable extra must be of a type supporting parcelable. | ||
+ | <code java> | ||
+ | package com.acme.myapp; | ||
+ | |||
+ | import android.os.Parcel; | ||
+ | import android.os.Parcelable; | ||
+ | |||
+ | public class Team implements Parcelable | ||
+ | { | ||
+ | public long id = 0; | ||
+ | public String name = ""; | ||
+ | public String number = ""; | ||
+ | public int ranking = 0; | ||
+ | |||
+ | // Default constructor | ||
+ | Team() | ||
+ | { | ||
+ | |||
+ | } | ||
+ | |||
+ | // In constructor, | ||
+ | // Important: Read them in the same sequence in which they were written in Parcel. | ||
+ | public Team(Parcel in) { | ||
+ | id = in.readLong(); | ||
+ | name = in.readString(); | ||
+ | number = in.readString(); | ||
+ | ranking = in.readInt(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public int describeContents() { | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | // Write member variables in Parcel. | ||
+ | // Write in any order. Not required to write all members in Parcel. | ||
+ | @Override | ||
+ | public void writeToParcel(Parcel dest, int flags) { | ||
+ | // Write data in any order | ||
+ | dest.writeLong(id); | ||
+ | dest.writeString(name); | ||
+ | dest.writeString(number); | ||
+ | dest.writeInt(ranking); | ||
+ | } | ||
+ | |||
+ | // De-serialize the object | ||
+ | public static final Parcelable.Creator< | ||
+ | public Team createFromParcel(Parcel in) { | ||
+ | return new Team(in); | ||
+ | } | ||
+ | |||
+ | public Team[] newArray(int size) { | ||
+ | return new Team[size]; | ||
+ | } | ||
+ | }; | ||
+ | } | ||
+ | </ | ||
=== Intent Examples === | === Intent Examples === | ||
For example, in the '' | For example, in the '' | ||
Line 229: | Line 311: | ||
case R.id.btnDialNumber: | case R.id.btnDialNumber: | ||
Intent intentDialNumber = new Intent(Intent.ACTION_DIAL, | Intent intentDialNumber = new Intent(Intent.ACTION_DIAL, | ||
- | startActivity(intentDialNumber); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
| | ||
case R.id.btnViewWebsite: | case R.id.btnViewWebsite: | ||
Intent intentViewWebsite = new Intent(Intent.ACTION_VIEW, | Intent intentViewWebsite = new Intent(Intent.ACTION_VIEW, | ||
- | startActivity(intentViewWebsite); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
| | ||
Line 240: | Line 326: | ||
Intent intentShowLocation = new Intent(Intent.ACTION_VIEW, | Intent intentShowLocation = new Intent(Intent.ACTION_VIEW, | ||
Uri.parse(" | Uri.parse(" | ||
- | )); | + | )); |
- | startActivity(intentShowLocation); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
| | ||
Line 247: | Line 335: | ||
// Display all activities supporting Action View that can handle intent | // Display all activities supporting Action View that can handle intent | ||
Intent intentShowAllActivities = new Intent(Intent.ACTION_VIEW); | Intent intentShowAllActivities = new Intent(Intent.ACTION_VIEW); | ||
- | startActivity(intentShowAllActivities); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
| | ||
Line 254: | Line 344: | ||
SampleActivity.class | SampleActivity.class | ||
); | ); | ||
- | startActivity(intentShowToast); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
| | ||
case R.id.btnShowSampleActivity: | case R.id.btnShowSampleActivity: | ||
Intent intentShowSampleActivity = new Intent(" | Intent intentShowSampleActivity = new Intent(" | ||
- | startActivity(intentShowSampleActivity); | + | |
+ | | ||
+ | } | ||
break; | break; | ||
} | } | ||
Line 789: | Line 883: | ||
== Database Connection == | == Database Connection == | ||
- | ==== DB Connector ==== | + | |
- | Download the appropriate Java database connector: | + | * [[swdev:android: |
- | | + | |
- | * Extract JAR file to some folder. | + | |
- | * Add JAR file as module: '' | + | |
- | * JAR package should be now listed under the project Gradle Scripts: '' | + | |
- | * In '' | + | |
- | include ': | + | |
- | include ': | + | |
- | </ | + | |
- | * In '' | + | |
- | dependencies { | + | |
- | //... | + | |
- | compile project(': | + | |
- | } | + | |
- | </ | + | |
- | * Alternatively, | + | |
- | * Right-click again on the app module folder, and select Open Module Settings. | + | |
- | * In the left navigation pane of the newly opened window, go to the category Modules and select app and then the Dependencies tab. | + | |
- | * Click on the green plus (+), and select Module Dependencies. Select the mysql module. | + | |
- | * Click OK, and close all the opened windows. A Synchronization process should take place once more. | + | |
- | * If everything has been done correctly, under your app module folder you should see a mysql-connector-java module folder. | + | |
- | * Include the classes required in your code. | + | |
- | * See also:[[swdev:java:Connecting to MYSQL Database]] | + | |
- | + | ||
- | ==== Manifest ==== | + | |
- | Add permission to access the Internet. | + | |
- | <code xml> | + | |
- | < | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | + | ||
- | ==== Database Strings ==== | + | |
- | Create a class to hold the database strings: | + | |
- | File '' | + | |
- | <code java> | + | |
- | package com.acme.myapp; | + | |
- | + | ||
- | public class DbStrings | + | |
- | { | + | |
- | static final String DATABASE_URL | + | |
- | static final String DATABASE_NAME = " | + | |
- | static final String USERNAME | + | |
- | static final String PASSWORD | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | ==== ItemAdapter ==== | + | |
- | Create class ItemAdapter | + | |
- | * Go to project tree, select '' | + | |
- | * Name: '' | + | |
- | * Kind: '' | + | |
- | * Superclass: '' | + | |
- | + | ||
- | <code java> | + | |
- | import android.view.ViewGroup; | + | |
- | import android.widget.BaseAdapter; | + | |
- | + | ||
- | public class ItemAdapter extends BaseAdapter | + | |
- | { | + | |
- | LayoutInflater mInflater; | + | |
- | Map< | + | |
- | + | ||
- | List< | + | |
- | List< | + | |
- | + | ||
- | public ItemAdapter(Context cx, Map m) | + | |
- | { | + | |
- | map = m; | + | |
- | products = new ArrayList< | + | |
- | prices | + | |
- | + | ||
- | mInflater = (LayoutInflater) cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public int getCount() | + | |
- | { | + | |
- | return map.size(); | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public Object getItem(int idx) | + | |
- | { | + | |
- | return products.get(idx); | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public long getItemId(int idx) | + | |
- | { | + | |
- | return idx; | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public View getView(int idx, View view, ViewGroup viewGroup) | + | |
- | { | + | |
- | // Use layout inflater. | + | |
- | + | ||
- | View vw = mInflater.inflate(R.layout.listview_detail, | + | |
- | TextView lblProduct = (TextView) vw.findViewById(R.id.lblProduct); | + | |
- | TextView lblPrice | + | |
- | + | ||
- | String strProduct = arrProducts.get(idx); | + | |
- | String strPrice | + | |
- | + | ||
- | lblProduct.setText(strProduct); | + | |
- | lblPrice.setText(strPrice); | + | |
- | + | ||
- | return vw; | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | === MainActivity === | + | |
- | <code java> | + | |
- | package com.acme.myapp; | + | |
- | + | ||
- | import android.support.v7.app.AppCompatActivity; | + | |
- | import android.os.Bundle; | + | |
- | + | ||
- | public class MainActivity extends AppCompatActivity | + | |
- | { | + | |
- | // Properties | + | |
- | ItemAdapter itemAdapter; | + | |
- | Context thisContext; | + | |
- | ListView lstProducts; | + | |
- | TextView lblProgress; | + | |
- | Map< | + | |
- | + | ||
- | @Override | + | |
- | protected void onCreate(Bundle savedInstanceState) | + | |
- | { | + | |
- | super.onCreate(savedInstanceState); | + | |
- | setContentView(R.layout.activity_main); | + | |
- | + | ||
- | Resources res = getResources(); | + | |
- | lstItems | + | |
- | lblProgress | + | |
- | thisContext | + | |
- | + | ||
- | lblProgress.setText("" | + | |
- | + | ||
- | Button bntConnect = (Button) findViewById(R.id.btnConnect); | + | |
- | btnConnect.setOnClickListener(new View.OnClickListener() { | + | |
- | @Override | + | |
- | public void onClick(View vw) { | + | |
- | GetData retrieveData = new GetDate(); | + | |
- | retrieveData.execute("" | + | |
- | } | + | |
- | }); | + | |
- | } | + | |
- | + | ||
- | private class GetData extends AsyncTask< | + | |
- | { | + | |
- | // Properties | + | |
- | String msg = ""; | + | |
- | + | ||
- | // JDBC driver name and database URL | + | |
- | static final String JDBC_DRIVER = " | + | |
- | + | ||
- | // Example: 192.168.0.2: | + | |
- | static final String DB_URL = " | + | |
- | DbStrings.DATABASE_URL + "/" | + | |
- | DbStrings.DATABASE_NAME; | + | |
- | + | ||
- | | + | |
- | | + | |
- | { | + | |
- | | + | |
- | } | + | |
- | + | ||
- | | + | |
- | | + | |
- | { | + | |
- | | + | |
- | | + | |
- | + | ||
- | try { | + | |
- | | + | |
- | conn = DriverManager.getConnection(DB_URL, | + | |
- | stmt = conn.createStatement(); | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | + | ||
- | | + | |
- | } | + | |
- | + | ||
- | msg = " | + | |
- | + | ||
- | | + | |
- | | + | |
- | | + | |
- | + | ||
- | } catch (SSQLException connError) { | + | |
- | msg = " | + | |
- | connError.printStackTrace(); | + | |
- | } catch (ClassNotFoundException ex) { | + | |
- | msg = "A class not foudnn exception was thrown."; | + | |
- | ex.printStackTrace(); | + | |
- | } finally { | + | |
- | try { | + | |
- | if (stmt != null) { stmt.close(); | + | |
- | } catch (SQLException ex) { | + | |
- | | + | |
- | } | + | |
- | try { | + | |
- | if (conn != null) { conn.close(); | + | |
- | } catch (SQLException ex) { | + | |
- | | + | |
- | } | + | |
- | } | + | |
- | | + | |
- | } | + | |
- | + | ||
- | | + | |
- | | + | |
- | | + | |
- | if (mapProducts.size() > 0) { | + | |
- | | + | |
- | | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | </ | + |