Good morning everybody, I'm trying to implement search features in a single activity with search features by implementing SearchView. From UI-side, everything is working.
But unlike almost of example I found, SearchView does not start another activity but is supposed so send request to the single running activity named HomeActivity.
Following some documentation I found on Android developers website and other sources, I try to do like below. Normally, I would supposed to handle search request in the event "onNewIntent" but it's never called. Click on the button after entering text has no effect (it just closes the keyboard).
I'm beginner in Android development and probably missed something simple.
Can you please help me ?
HomeActivity
public class HomeActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private FrameLayout myFrameLayout;
private Fragment fragmentContracts, fragmentHardware, fragmentMaps, fragmentUsers;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment fragment = null;
switch (item.getItemId()) {
// Switch to fragment ...
}
return loadFragment(fragment);
}
};
private void handleIntent(Intent intent) {
Log.i("LOG_myapp", "Called new intent !!!");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
//use the query to search your data somehow
String msg = MessageFormat.format("Search query = {0}", query);
Log.i("LOG_myapp", msg);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(query)
.setTitle("Search request");
AlertDialog dialog = builder.create();
dialog.show();
}
}
#Override
protected void onNewIntent(Intent intent){
// *** NEVER CALLED :( ***
Log.i("LOG_myapp", "Event onNewIntent raised");
handleIntent(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
myFrameLayout = (FrameLayout)findViewById(R.id.MyFrameLayout);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Fragment defaultFragment = new places();
defaultFragment.setRetainInstance(true);
loadFragment(defaultFragment);
Toolbar toolbar = (Toolbar)findViewById(R.id.activity_main_toolbar);
setSupportActionBar(toolbar);
handleIntent(getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.application, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.MenuItemSearch).getActionView();
searchView.setSubmitButtonEnabled(true);
ComponentName componentName = new ComponentName(this, HomeActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.MenuItemSettings:
Intent intent = new Intent(this, preferences.class);
startActivity(intent);
break;
}
return true;
}
#Override
public boolean onQueryTextChange(String s){
Log.i("LOG_myapp","Search query: " + s);
return true;
}
#Override
public boolean onQueryTextSubmit(String s){
Log.i("LOG_myapp","Submitted search query: " + s);
return true;
}
private boolean loadFragment(Fragment fragment) {
//switching fragment
if (fragment != null) {
try {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.MyFrameLayout, fragment);
transaction.addToBackStack(null);
transaction.commit();
return true;
}
catch (Exception ex)
{
return false;
}
}
return false;
}
}
Menus.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/MenuItemSearch"
android:title="Search"
android:icon="#drawable/baseline_search_white_18dp"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
<item android:id="#+id/MenuItemSettings"
android:icon="#drawable/baseline_settings_white_18dp"
app:showAsAction="always"
android:title="Settings"/>
</menu>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vermilionenergy.myapp">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".HomeActivity"
android:label="#string/app_name"
android:launchMode="singleTop">
<intent-filter>
<!--action android:name="android.intent.action.MAIN" /-->
<action android:name="android.intent.action.SEARCH"/>
<category android:name="android.intent.category.DEFAULT" />
<!-- category android:name="android.intent.category.LAUNCHER" / -->
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable"/>
<meta-data android:name="android.app.default_searchable" android:value=".HomeActivity" />
</activity>
<meta-data android:name="android.app.default_searchable" android:value=".HomeActivity" />
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".maps"
android:label="#string/title_activity_maps" />
<activity
android:name=".preferences"
android:label="Preferences" />
<!-- activity android:name=".users" / -->
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TextActivity"></activity>
</application>
</manifest>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="myapp"
android:hint="Search items"
>
</searchable>
I finally found the solution myself.
It looks like the direct strings in searchable.xml don't work. I replaced them by variable as below and it worked :)
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_title"
android:hint="#string/search_hint"
>
</searchable>
Related
I have completely followed the Docs on this regard.
I made searchable.xml file
added new activity to handle the query once search is made.
I updated the manifest file accordingly.
My problem is that the new activity is not being called at all as if there is something wrong with the manifest.
Here's the xml file:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
Here's the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sultanraja.notes">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity android:name=".NoteActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And here's the new activity code meanwhile:
public class SearchResultsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Toast.makeText(getApplicationContext(),"your Query is " + query, Toast.LENGTH_SHORT).show();
//use the query to search your data somehow
}
}
}
The Toast is never happening and a breakpoint in the onCreate method is never reached.
what is the problem?
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
if(list.contains(query)){
adapter.getFilter().filter(query);
}else{
Toast.makeText(MainActivity.this, "No Match found",Toast.LENGTH_LONG).show();
}
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// adapter.getFilter().filter(newText);
return false;
}
});
I found the problem in the manifest file:
I added the following:
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
this part was added to mainActivity and that was not part of the documentation.
thanks.
I'm programming an android app with Android Studio where Imagebutton opens a new screen so that the user can proceed to the next step. Imagebutton is working and new screen opens, but it isn't opening second activity but the same one.
I'm new to android and java programming, so please tell me if other codes are necessary for solving the problem and sorry if I shared unnecessary code.
I have created startActivity with intent.
Here are my current codes of MainActivity java-file and AndroidManifest xml-file:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
mTextMessage = findViewById(R.id.message);
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
final ImageButton imgButton = findViewById(R.id.simpleImageViewHowdoglearnspackage);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobidogi">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SecondActivity"
android:label="#string/title_activity_second" />
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I am totally new to android development basically I am a web developer, I done a Udemy course about converting a wordpress site to android webview app. Which I followed and made a app by just copying the code from files. Now when ever I open the app I see a white screen which sometimes give feeling that app is not working but the app is loading web page. Now I would like to add a loading image or loader any thing.
Here is my code of mainActivity.java
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://bikanershop.com/");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of
//WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
Parse.initialize(this, "oI7lWjyQc0EhpDsn1cyfaoCtpUbKQp1rFbX6PPZN", "FIeGPOxqKe3jBuvXKjW4Ml69K12tjDRq6sLruqUQ");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
another file is myAppWebViewClient.java
public class MyAppWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(Uri.parse(url).getHost().endsWith("bikanershop.com"))
{
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
and xml file is as below AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bikanershop.bikanershop" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change
"com.parse.tutorials.pushnotifications.permission.C2D_MESSAGE" in the
lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.bikanershop.bikanershop.permission.C2D_MESSAGE" />
<uses-permission
android:name="com.bikanershop.bikanershop.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action
android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.tutorials.pushnotifications" to
match your app's package name.
-->
<category android:name="com.bikanershop.bikanershop" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
I am using android studio latest version that is 1.1.0
Use a ProgressDialog, as outlined Here.
For your case, since you need to close the dialog, you should just in-line your functionality from myAppWebViewClient.java.
So your new code would be something like this:
public class MainActivity extends ActionBarActivity {
private ProgressDialog dialog = new ProgressDialog(WebActivity.this);
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(Uri.parse(url).getHost().endsWith("bikanershop.com"))
{
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
});
dialog.setMessage("Loading..Please wait.");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://bikanershop.com/");
// Force links and redirects to open in the WebView instead of in a browser
//mWebView.setWebViewClient(new WebViewClient()); //replaced with code above
// Stop local links and redirects from opening in browser instead of
//WebView
//mWebView.setWebViewClient(new MyAppWebViewClient());
Parse.initialize(this, "oI7lWjyQc0EhpDsn1cyfaoCtpUbKQp1rFbX6PPZN", "FIeGPOxqKe3jBuvXKjW4Ml69K12tjDRq6sLruqUQ");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
I have a confusing problem. I have a MainActivity with 2 actions : Update and Logout. The problem is when I run the activity that extends ListActivity the action bar doesn't appear.
Below I have 2 images with 2 different extend types in MainActivity
Extending ActionBarActivity example
public class MainActivity extends ActionBarActivity
By extends ListActivity the result is the same as in the picture below. Basically I want to make the main activity with a ListView and an action bar so that the user is able to update and logout using the action bar. But it seems it doesn't work and i need your help. I tried searching on the web i couldn't find anything that helped.
public class MainActivity extends ListActivity
Here you can see my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.florin.statusapp" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk android:minSdkVersion="11"
android:targetSdkVersion="21"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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>
<activity
android:name=".RegisterActivity"
android:label="#string/title_activity_register" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name=".UpdateStatusActivity"
android:label="#string/title_activity_update_status" >
</activity>
</application>
</manifest>
My MainActivity.java
public class MainActivity extends ListActivity{
private List<ParseObject> mStatusObjects;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Enable Local Datastore.
Parse.initialize(this, "foo", "bar");
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
} else {
// show the login screen
Intent toLoginActivity = new Intent(MainActivity.this, LoginActivity.class);
startActivity(toLoginActivity);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_main, menu);
//return true;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
switch (id) {
case R.id.updateStatus:
// take user to update activity
Intent toMainActivityIntent = new Intent(MainActivity.this, UpdateStatusActivity.class);
startActivity(toMainActivityIntent);
break;
case R.id.LogoutUser:
//Log out user
ParseUser.logOut();
// take user to login activity
Intent toLoginActivityIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(toLoginActivityIntent);
break;
}
return super.onOptionsItemSelected(item);
}
and the menu_main.xml for the action bar:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.florin.statusapp.MainActivity">
<item android:id="#+id/updateStatus"
android:title="Update"
app:showAsAction="always" />
<item
android:id="#+id/LogoutUser"
android:title="Logout"
app:showAsAction="never"
/>
</menu>
This should be related to your theme. Action bars are only supported on themes after holo.
http://developer.android.com/guide/topics/ui/actionbar.html#Adding
Your styles.xml probably has something like:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
You can change it to this to use the holo theme:
<style name="AppTheme" parent="android:Theme.Holo">
As Tachyonflux said, on API 11 and higher, the action bar is included in all activities that use Theme.Holo or one of it's descendants
Try adding the following to your AndroidManifest.xml
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo">
Or another Theme or your choosing. Go to the link Tachyonflux has and look at the available options. There are various default options, but you can also create your own.
I have created a sample android application for receiving push messages from Parse. I had followed this video tutorial. The application is working fine and I am able to receive push notifications.
The classes required are as follows:
ParseApplication class:
public class ParseApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
// Add your initialization code here
Parse.initialize(this, "ygdC4y3uzCe0TOEPOfB1U469Gg5ZJGxF2OGlNKCG", "Tk5ZlDiWWVJm2Xio5IiXk0KI5JM1jrvGPOSpFxzE");
PushService.setDefaultPushCallback(ParseApplication.this, SampleClass.class);
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
// If you would like all objects to be private by default, remove this line.
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
}
ParseStarterProjectActivity class:
public class ParseStarterProjectActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ParseAnalytics.trackAppOpened(getIntent());
}
}
SampleClass class:
public class SampleClass extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_class);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sample_class, menu);
return true;
}
}
The manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parse.starter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission
android:name="com.parse.starter.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.parse.starter.permission.C2D_MESSAGE" />
<application
android:name="com.parse.starter.ParseApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.parse.starter.ParseStarterProjectActivity"
android:label="Test Push" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.parse.starter.MainActivity"
android:label="Test Push" >
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
<category android:name="com.parse.starter" />
</intent-filter>
</receiver>
<activity
android:name="com.parse.starter.SampleClass"
android:label="#string/title_activity_sample_class" >
</activity>
</application>
</manifest>
Now I want to get text from the push notifications when the user receives them. What should I do to get the text from the notifications?
This is the url to the push notification server.
I even went though this.
I had to change the SampleClass Activity like this:
public class SampleClass extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_class);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String jsonData = extras.getString( "com.parse.Data" );
Log.i("Data Received:", jsonData);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sample_class, menu);
return true;
}
The rest is as it is. I referred this link.