Sie sind auf Seite 1von 22

Java Android Program to Display a calendar event

using an Intent
Here is source code of the Program to Display a calendar event using an Intent in
Andorid. The program is successfully compiled and run on a Windows system using
Eclipse Ide. The program output is also shown below.
MainActivity.java
package com.example.displaycalendareventintent;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button disp = (Button) findViewById(R.id.dispbut);
disp.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
disp();
}
});
}

public void disp() {


Calendar startTime = Calendar.getInstance();
startTime.set(2013, 2, 13, 11, 35);
Uri uri = Uri.parse("content://com.android.calendar/time/"
+ String.valueOf(startTime.getTimeInMillis()));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// Use the Calendar app to view the time.
startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/dispbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Display Event" />

</RelativeLayout>

Java Android Program to Demonstrate Deleting a


Calendar Event in Android
Here is source code of the Program to Demonstrate Deleting a Calendar Event in
Android. The program is successfully compiled and run on a Windows system using
Eclipse Ide. The program output is also shown below.
MainActivity.java
package com.example.deletecaendarevent;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract.Events;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button del = (Button) findViewById(R.id.delbut);
del.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
delevent();
}
});
}

@SuppressLint("NewApi")
public void delevent() {

final String DEBUG_TAG = "MyActivity";


long eventID = 760;
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
Uri deleteUri = null;
deleteUri = ContentUris.withAppendedId(Events.CONTENT_URI,
eventID);
int rows = getContentResolver().delete(deleteUri, null, null);
Log.i(DEBUG_TAG, "Rows deleted: " + rows);
Toast.makeText(this, "Event deleted", Toast.LENGTH_LONG).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/delbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="188dp"
android:text="Delete Event" />

</RelativeLayout>

Java Android Program to Demonstrate Voice Search


in a Search Provider
Here is source code of the Program to Demonstrate Search Interface with
Suggestion Provider in Android. The program is successfully compiled and run on a
Windows system using Eclipse Ide. The program output is also shown below.
When you’re ready to add search functionality to your application, Android
helps you implement the user interface with either a search dialog that
appears at the top of the activity window or a search widget that you can
insert in your layout. Both the search dialog and the widget can deliver the
user’s search query to a specific activity in your application. This way, the
user can initiate a search from any activity where the search dialog or
widget is available, and the system starts the appropriate activity to perform
the search and present results.
Other features available for the search dialog and widget include:
1.Voice search
2.Search suggestions based on recent queries
3.Search suggestions that match actual results in your application data
To know more go to the following link-
http://developer.android.com/guide/topics/search/search-dialog.html
MainActivity.java
package com.example.searchinterface;

import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class MainActivty extends Search {


@Override
ListAdapter makeMeAnAdapter(Intent intent) {
return(new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items));
}
}
Search.java
package com.example.searchinterface;

import android.os.Bundle;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;

abstract public class Search extends ListActivity {


abstract ListAdapter makeMeAnAdapter(Intent intent);

private static final int LOCAL_SEARCH_ID = Menu.FIRST + 1;


private static final int GLOBAL_SEARCH_ID = Menu.FIRST + 2;
EditText selection;
ArrayList<String> items = new ArrayList<String>();

@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
selection = (EditText) findViewById(R.id.selection);

try {
XmlPullParser xpp = getResources().getXml(R.xml.words);

while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {


if (xpp.getEventType() == XmlPullParser.START_TAG) {
if (xpp.getName().equals("word")) {
items.add(xpp.getAttributeValue(0));
}
}
xpp.next();
}
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(), 4000)
.show();
}

setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
onNewIntent(getIntent());
}

@Override
public void onNewIntent(Intent intent) {
ListAdapter adapter = makeMeAnAdapter(intent);
if (adapter == null) {
finish();
} else {
setListAdapter(adapter);
}
}

public void onListItemClick(ListView parent, View v, int position,


long id) {

selection.setText(parent.getAdapter().getItem(position).toString());

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, LOCAL_SEARCH_ID, Menu.NONE, "Local Search")
.setIcon(android.R.drawable.ic_search_category_default);
menu.add(Menu.NONE, GLOBAL_SEARCH_ID, Menu.NONE, "Global Search")
.setIcon(R.drawable.search)
.setAlphabeticShortcut(SearchManager.MENU_KEY);

return (super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case LOCAL_SEARCH_ID:
onSearchRequested();
return (true);

case GLOBAL_SEARCH_ID:
startSearch(null, false, null, true);
return (true);
}
return (super.onOptionsItemSelected(item));
}
}
SearchInterface
package com.example.searchinterface;

import java.util.ArrayList;
import java.util.List;

import android.app.SearchManager;
import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class SearchInterface extends Search {


@Override
ListAdapter makeMeAnAdapter(Intent intent) {
ListAdapter adapter=null;

if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
String query=intent.getStringExtra(SearchManager.QUERY);
List<String> results=searchItems(query);
adapter=new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,results);
setTitle("Search : "+query);
}
return(adapter);
}

private List<String> searchItems(String query) {


SearchSuggestionProvider
.getBridge(this)
.saveRecentQuery(query, null);

List<String> results=new ArrayList<String>();

for (String item : items) {


if (item.indexOf(query)>-1) {
results.add(item);
}
}
return(results);
}
}
SuggestionProvider
package com.app.searchinterfacedemo;

import android.content.Context;
import android.content.SearchRecentSuggestionsProvider;
import android.provider.SearchRecentSuggestions;

public class SuggestionProvider extends SearchRecentSuggestionsProvider {


static SearchRecentSuggestions getBridge(Context ctxt) {
return (new SearchRecentSuggestions(ctxt,
"com.example.MainActivity", DATABASE_MODE_QUERIES));
}
public SearchSuggestionProvider() {
super();
setupSuggestions("com.app.SearchInterfaceDemo",
DATABASE_MODE_QUERIES);
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="60dp" />

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />

</LinearLayout>
Include these two in /res/xml.
searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/Hint"
android:includeInGlobalSearch="true"
android:label="@string/Label"
android:searchSettingsDescription="@string/global"
android:searchSuggestAuthority="com.app.SearchInterfaceDemo"
android:searchSuggestSelection=" ? "
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />
Word.xml

<words>

<word value="C" />


<word value="gcc" />
<word value="C++" />
<word value="codeBlocks" />
<word value="Java" />
<word value="NetBeans" />
<word value="Eclipse" />
<word value="Android" />
<word value="Eclipse" />
<word value="PHP" />
<word value="MIPS" />
<word value="SQL" />
<word value="SQLite" />
<word value="XML" />
<word value="C#" />
<word value="Adodbe" />
<word value="MATLAB" />
<word value=".NET" />
<word value="Google" />

</words>
Add the following in your Manifest in

..

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
Add the following in your Manifest in

<activity>
..
</activity>
Meta Data for Your Activity

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<application>
....
</application>
Defining Suggestion Provider in your Application –

<provider
android:name=".SearchSuggestionProvider"
android:authorities="com.example.searchinterface.SuggestionProvider" />

Java Android Program to Demonstrate a Full Screen


Activity
Here is source code of the Program to Demonstrate a Full Screen Activity in
Andorid. The program is successfully compiled and run on a Windows system using
Eclipse Ide. The program output is also shown below.
MainActivity.java

package com.example.fullscreen;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

EditText distance, result;


RadioGroup rg1;
Button calculate;
private double fare = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setting our activity to be full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

}
}
Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/farecard"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:background="@android:color/darker_gray"
android:text="CALCULATE"
android:textAlignment="center"
android:textColor="@android:color/black" />

<EditText
android:id="@+id/auto_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="26dp"
android:ems="10" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/auto_result"
android:layout_alignRight="@+id/textView1"
android:text="Your Total Fare"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20dp" />

<EditText
android:id="@+id/auto_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_marginBottom="18dp"
android:ems="10" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Enter Distance"
android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignLeft="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_marginBottom="14dp"
android:orientation="horizontal"
android:paddingBottom="40px" >

<RadioGroup
android:id="@+id/auto_rg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >

<RadioButton
android:id="@+id/auto_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="84dp"
android:layout_weight="1"
android:text="DAY" />

<RadioButton
android:id="@+id/auto_night"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="84dp"
android:layout_weight="1"
android:text="NIGHT" />
</RadioGroup>
</LinearLayout>

<ImageButton
android:id="@+id/auto_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout1"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:src="@drawable/phone" />

<ImageButton
android:id="@+id/auto_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/auto_call"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/arrow" />

</RelativeLayout>

Java Android Program to Demonstrate Finding


Contact Details for a Contact name
Here is source code of the Program to Demonstrate Finding contact details for a
contact name. The program is
successfully compiled and run on a Windows system using Eclipse Ide. The
program output is also shown below.
The following code shows how to use the contact-detail column names
available in the CommonDataKinds subclasses to extract the display name
and mobile phone number from the Data table for a particular contact.
MainActivity.java
package com.example.contactdetailcontactname;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button detail = (Button) findViewById(R.id.butDetails);
detail.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
findDetail();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void findDetail() {


ContentResolver cr = getContentResolver();
String[] result = null;
// Find a contact using a partial name match
String searchName = "specify serach name here";
Uri lookupUri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_FILTER_URI, searchName);
// Create a projection of the required column names.
String[] projection = new String[] { ContactsContract.Contacts._ID
};
// Get a Cursor that will return the ID(s) of the matched name.
Cursor idCursor = cr.query(lookupUri, projection, null, null,
null);
// Extract the first matching ID if it exists.
String id = null;
if (idCursor.moveToFirst()) {
int idIdx = idCursor
.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
id = idCursor.getString(idIdx);
}
// Close that Cursor.
idCursor.close();
// Create a new Cursor searching for the data associated with the
// returned Contact ID.
if (id != null) {
// Return all the PHONE data for the contact.
String where = ContactsContract.Data.CONTACT_ID + " = " + id
+ "AND" + ContactsContract.Data.MIMETYPE + " = ‘"
+
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
+ "’";
projection = new String[] {
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor dataCursor = getContentResolver().query(
ContactsContract.Data.CONTENT_URI, projection, where,
null,
null);
// Get the indexes of the required columns.
int nameIdx = dataCursor

.getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME);
int phoneIdx = dataCursor

.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
result = new String[dataCursor.getCount()];
while (dataCursor.moveToNext()) {
// Extract the name.
String name = dataCursor.getString(nameIdx);
// Extract the phone number.
String number = dataCursor.getString(phoneIdx);
result[dataCursor.getPosition()] = name + "(" + number +
")";
Toast.makeText(this, name + "(" + number + ")",
Toast.LENGTH_SHORT).show();

}
dataCursor.close();
}
}
}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/butDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="100dp"
android:text="Find Details" />

</RelativeLayout>
AndoridManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contactdetailcontactname"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity

android:name="com.example.contactdetailcontactname.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>

</manifest>

Das könnte Ihnen auch gefallen