Google Analytics Campaign Tracking: Testing - java

I am trying to implement google analytics campaign tracking into my app. The relevant code is:
MainActivity.java
package com.example.prakhar.myapplication;
import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View;
import com.google.android.gms.analytics.HitBuilders; import com.google.android.gms.analytics.Tracker;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Tracker t = ((AnalyticsApplication) getApplication()).getTracker(AnalyticsApplication.TrackerName.APP_TRACKER);
String campaignData = "http://examplepetstore.com/index.html?" +
"utm_source=email&utm_medium=email_marketing&utm_campaign=summer" +
"&utm_content=email_variation_1";
Log.d("MainActivity", "Sending an event");
t.send(new HitBuilders.ScreenViewBuilder()
.setCampaignParamsFromUrl(campaignData)
.build()
);
Log.d("MainActivity", "Event sent");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_SHORT)
.setAction("Action", null).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.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);
} }
AnalyticsApplication.java
package com.example.prakhar.myapplication;
import android.app.Application;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;
import java.util.HashMap;
/**
* Created by prakhar on 2/12/15.
*/
public class AnalyticsApplication extends Application{
private Tracker mTracker;
// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-XXXX-XX";
/**
* Enum used to identify the tracker that needs to be used for tracking.
*
* A single tracker is usually enough for most purposes. In case you do need multiple trackers,
* storing them all in Application object helps ensure that they are created only once per
* application instance.
*/
public enum TrackerName {
APP_TRACKER,
GLOBAL_TRACKER,
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
: analytics.newTracker(R.xml.app_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
synchronized public Tracker getDefaultTracker()
{
if (mTracker == null)
{
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
mTracker = analytics.newTracker(R.xml.global_tracker);
}
return mTracker;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.prakhar.myapplication" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:name="com.example.prakhar.myapplication.AnalyticsApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Used for Google Play Store Campaign Measurement-->
<receiver android:name="com.google.android.gms.analytics.AnalyticsReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.AnalyticsService"
android:enabled="true"
android:exported="false"/>
<!-- Optionally, register CampaignTrackingReceiver and CampaignTrackingService to enable
installation campaign reporting -->
<receiver android:name="com.google.android.gms.analytics.CampaignTrackingReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
</intent-filter>
</receiver>
<service android:name="com.google.android.gms.analytics.CampaignTrackingService" />
</application>
</manifest>
app_tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="ga_sessionTimeout">300</integer>
<bool name="ga_autoActivityTracking">true</bool>
<string name="ga_trackingId">UA-XXXXXX </string>
<string name="ga_sampleFrequency">100.0</string>
<bool name="ga_reportUncaughtExceptions">true</bool>
</resources>
Essentially, I should get a log as soon as I install the app, but that is not happening. Where am I going wrong? Am I testing it the wrong way?
Initially I was getting the ClassCastException but that I resolved by including the name in the android manifest.

I am not sure, but on web pages, when you install analytics you have to wait more or less 1/2 days to begin to work.

Related

Android BroadcastReceiver in ScrollActivity

I had working BroadcastReceiver which notifys user about network status changes, but since i added ScrollActivity to my app, the receiver is not working anymore. So i tried to make a blank ScrollActivity template project to test the reciever and i can see the same - the receiver wont work in apps with ScrollActivity. im a little bit desperate with this situation, would be thankful for help.
I have permissions and receiver registred in manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_app">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<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=".ScrollingActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".ConnectivityStatusReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
</manifest>
Here comes the receiver code, which hasnt been called even once in scrollactivity apps yet..
package com.example.test_app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class ConnectivityStatusReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//THIS HASNT BEEN CALLED EVEN ONCE YET SINCE SCROLLACTIVITY
final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connMgr.getActiveNetworkInfo();
Log.d("RECIEVER_LOG", "intent:"+intent.getAction());
if(activeNetworkInfo.isConnected()){
Log.d("NETWORK_LOG", "connected.");
}else{
Log.d("NETWORK_LOG", "not connected.");
}
if (activeNetworkInfo == null){
//no connection activity opens on connection loss
context.startActivity(new Intent(context , NoInternetConnection.class));
}
}
}
And here is the Scrolling activity itself, untouched as Android Studio generated it
package com.example.test_app;
import android.os.Bundle;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class ScrollingActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CollapsingToolbarLayout toolBarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
toolBarLayout.setTitle(getTitle());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).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.menu_scrolling, 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);
}
}
Would be thankful for any help.

Boot Receiver doesn't work

First off, please don't mark this as duplicate, I have tried to resolve the issue using several different questions here on SO, and tried each and every solution for this issue, over SO.
Therefore, I reproduce my entire code here along with the logcat.
The Issue:
I am trying to write an application that will boot up on reboot of a device.
I can see several applications receiving the BOOT_COMPLETED action in the logcat, but I can't see my application anywhere in logcat on device reboot.
Point to Note:
I have already launched my app once before testing through Device Reboot.
Code Files:
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz.abc"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="com.xyz.abc.autostart" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".hello"
android:label="#string/title_activity_hello" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".service"
android:enabled="true" />
</application>
</manifest>
Autostart.java
package com.xyz.abc;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by admin on 008, 8 May 2015.
*/
public class autostart extends BroadcastReceiver
{
public void onReceive(Context context, Intent arg1)
{
Log.w("boot_broadcast_poc", "starting service...");
context.startService(new Intent(context, service.class));
}
}
service.java
package com.xyz.abc;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
/**
* Created by admin on 008, 8 May 2015.
*/
public class service extends Service
{
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
#Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(this, "NotifyingDailyService", Toast.LENGTH_LONG).show();
Log.i("bootbroadcastpoc","NotifyingDailyService");
return super.onStartCommand(pIntent, flags, startId);
}
}
hello.java
package com.xyz.abc;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class hello extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
Toast.makeText(getBaseContext(), "Hello........", 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.menu_hello, 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);
}
}
Finally, the logcat filtered by BOOT_COMPLETED:
UPDATE : Removed the logcat as it was not necessary
(Also I have found the solution which I will have posted down shortly).
logcat filtered for "boot_broadcast_poc" is blank.
logcat filtered for "bootbroadcastpoc" is blank.
I definitely don't see the service started as I don't see any Toast on the screen on boot.
As stated in comments, I was testing on a Redmi 1s, which is a Xiaomi phone, running MIUI.
I tested this same piece code on an emulator (Bluestacks to be precise) and it worked like a charm. This is probably why nobody had a solution! There's no problem in this code!
Turns out, as mentioned here, that the MIUI (my Android version is 4.3), needs another set of permission which is:
<action android:name="android.intent.action.REBOOT"/>
in the Manifest.
Secondly, MIUI seems to have set special permissions to auto-start the app - which can be set in here:
"Settings > Apps > YOUR_APP > Manage permissions"
There you need to enable "Autostart" option.
Hope it helps someone using Xiaomi's Redmi / Mi devices.
P.S. My test device was running MIUI-JHCMIBH45.0 when I encountered this issue.

Error and have no clue why :(

i have been trying to figure out how to make a simple search button. I found this (Implementing SearchView in action bar) which i thought would help and i guess it did, i just cant figure out why there is an error here:
SearchableActivity.java
package com.ryan.buttonsimple;
import android.app.SearchManager;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
public class SearchableActivity extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) findItemById(R.id.search_view); //**HERE IS THE ERROR! "findItemById" and "search_view" "Cannot resolve method for both**
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default
}
#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_searchable, 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);
}
}
here is my searchable.xml file:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint">
</searchable>
And here is my AndroidManifest.xml File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ryan.buttonsimple" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
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=".SearchableActivity"
android:label="#string/title_activity_searchable" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
</application>
</manifest>

Send Array to Different Application

I would like to send an array in different application.
So In the first application I have MainActivity. The Activity will start an intent. When I'm trying to make an intent, I found a problem. The problem is ShowArrayActivity "cannot be resolved to a type". Actually ShowArrayActivity class is in another apppication. I have tried to add project in build path, but I have an error message said that a cycle was detected in the build path of project 'sendIntent'..
package com.example.sendintent;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import com.example.sendintent.DisplayMessageActivity;
public class MainActivity extends Activity {
public static final String ARRAYS_COUNT = "com.example.sendintent.MainActivity.ARRAYS_COUNT";
public static final String ARRAY_INDEX = "com.example.sendintent.MainActivity.ARRAY_INDEX";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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 sendMessage(View view)
{
final String data[][] = new String[][]
{{"Car","75","loaded"},
{"Motor","25","loaded"},
{"Truck","30","loaded"},
{"Bike","40","loaded"},
{"Boat","10","loaded"}};
Bundle bundle = new Bundle();
int count = data.length;
bundle.putInt(ARRAYS_COUNT, count);
for (int i = 0; i < count; i++)
bundle.putStringArray(ARRAY_INDEX + i, data[i]);
Intent intent = new Intent(this, ShowArrayActivity.class); ---->ERROR
intent.putExtras(bundle);
startActivity(intent);
}
}
In Manifest I have declare
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendintent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.sendintent.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="com.example.sendintent.DisplayMessageActivity"
android:parentActivityName="com.example.sendintent.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.sendintent.MainActivity" />
</activity>
<service android:name="com.example.sendintent.MainActivity" />
<receiver android:name="com.example.receiver.MainActivityReceiver">
<intent-filter>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_COUNT"/>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_INDEX"/>
</intent-filter>
</receiver>
</application>
</manifest>
In another application, I have declare the receiver for the intent. It called MainActivityReceiver
package com.example.receiver;
import java.util.ArrayList;
import com.example.sendintent.MainActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MainActivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
intent.setClass(context, MainActivity.class);
context.startService(intent);
}
}
I have another class called ShowArrayAcivity:
package com.example.receiver;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class ShowArrayActivity extends Activity {
public String[][] data;
public static final String ARRAYS_COUNT = "com.example.sendintent.MainActivity.ARRAYS_COUNT";
public static final String ARRAY_INDEX = "com.example.sendintent.MainActivity.ARRAY_INDEX";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
int count = bundle.getInt(ShowArrayActivity.ARRAYS_COUNT, 0);
ArrayList<String[]> arrays = new ArrayList<String[]>(count);
for (int i = 0; i < count; i++)
arrays.add(bundle.getStringArray(ShowArrayActivity.ARRAY_INDEX + i));
data = arrays.toArray(new String[][]{});
}
}
#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;
}
}
In Manifest for receiver application
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.receiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.receiver.ShowArrayActivity"
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.example.sendintent.MainActivity" />
<receiver android:name="com.example.receiver.MainActivityReceiver">
<intent-filter>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_COUNT"/>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_INDEX"/>
</intent-filter>
</receiver>
</application>
</manifest>
In receiver application, It won't show the array from sendIntent application in Interface. I just want to know the way to use broadcast and intent. So if I run in debug mode,in ShowArrayActivity, the variable data will containt the result of the intent.
So my questions is,why I can't use another class when I try to send the intent? Did I forgot something to implement for madethe communication between 2 application?
Intent intent = new Intent(this, ShowArrayActivity.class);
this way you tell activity to find ShowArrayActivity within this Application and launch it. It fails as Application can't find it in it's manifest.
Create Intent with action.
Intent intent = new Intent (NAME_OF_THE_FILTER);
then replace string it second manifest
assuming
public static final String NAME_OF_THE_FILTER = "com.myapp.myfilter"
<intent-filter>
<action android:name="com.myapp.myfilter"/>
</intent-filter>

Errors in Android Studio myfirstapp : Gradle errors

I'm trying to follow along with the official Android tutorial (http://developer.android.com/training/basics/firstapp/starting-activity.html) but I'm failing miserably. Everything worked OK until I needed to add a second Activity. First, the Activity I created in Android Studio does not show up in my list of java files. I clicked New -> Activity and nothing appeared. To get around this I opened up Windows Explorer and copied/renamed MainActivity.java to DisplayMessageActivity.java and added the code in the tutorial.
After following along the app does not run and I get multiple "Gradle" errors such as:
Gradle: error: cannot find symbol class Activity
Gradle: error: cannot find symbol class Bundle
What do I need to fix in order to get this to run? Here is the relevant code:
MainActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
}
Display MessageActivity.java
package com.example.myfirstapp;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="#string/app_name"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
when you create an activity, make sure you insert it into mainifest.xml as well, so your manifest should be like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="#string/app_name"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.MessageActivity>
</activity>
</application>
</manifest>
On each error you got (on red as on image) go and press Alt+Enter to import missing classes or declare the variables.
Please note that all files (.java) have to be not red underlined if they are error free.
Maybe some tutorial´s names of classes´s changed. then you need put the name your Activity before the variable "extra_message" like that MyActivity.EXTRA_MESSAGE
on the main Activity put...
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
or simple use any value. like that
String message = intent.getStringExtra('xxx');
Go on through the tutorial, you'll be taught to create one later.

Categories

Resources