EDIT: It works now
It does work now, i changed "Database.java" to "database.java" and just kept the normal
Intent startListItem = new Intent("com.grumbledorf.Hellotraining." + item.toUpperCase());
and
<activity
android:name=".databases"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.grumbledorf.Hellotraining.DATABASES" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
So thanks guys!
You are trying to start the activity "com.grumbledorf.Hellotraining.database" and as I see the proper activity name would be "com.grumbledorf.Hellotraining.Databases".
proper stuff definition:
String[] stuff = new String[] { "Databases" };
But a full Manifest.xml would be helpful.
The Intent creation using reflection:
Intent startListItem = new Intent(this, Class.forName("com.grumbledorf.Hellotraining." + item.toUpperCase()));
have you tried
Intent startListItem = new Intent("com.grumbledorf.Hellotraining","com.grumbledorf.Hellotraining." + item.toUpperCase());
?
Related
I'm making a pdf file viewer app with this library. I applied other things successfully, but I'm continuously failing to link the app with "Complete action using" list of Android.
First, init() method of the library is as follows(MainActivity.java):
private void init() {
lv_pdf = (ListView)findViewById(R.id.lv_pdf);
dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
fn_permission();
lv_pdf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), PdfActivity.class);
intent.putExtra("position", i);
startActivity(intent);
Log.e("Position", i + "");
}
});
}
Second, AndroidManifest.xml file is as follows:
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PdfActivity"
android:screenOrientation="portrait">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
MainActivity.java defines permission and start screens of the app, and PdfActivity.java shows content of the pdf files.
The Problem:
init() method have a implicit intent with getApplicationContext(). How to I link this to intent filter in AndroidManifest.xml file? I want to launch the app through "Complete action using" screens.
Too can you use it
Intent intent = new (this, PdfActivity.class);
Instead of getApplicationContext() use MainActivity.this
I created an aar and i added it to my project as an module. in this module i have a HelloWorldActivity that i want to run.
my module manifest looks like this.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="ir.sibvas.testlibary1.HelloWorldActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="ir.sibvas.testlibary1.HelloWorldActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Now i can start this activity from my project using this code
Intent intent = new Intent("ir.sibvas.testlibary1.HelloWorldActivity");
startActivity(intent);
but as you can see this code is implicit and problem with implicit calling is that if i use this module in more than one app, both installed on user device it will show an app chooser dialog to user. So how can make this call explicit, preventing user from switching app?
this code will not run since HelloWorldActivity is not in the same package as calling activity
Intent intent = new Intent(this, HelloWorldActivity.class);
startActivity(intent);
I really don't want to change my module for each project that uses it.
You can use the Class.forName(), it worked for me when i was needed to start activity which is in another module in my project.
Intent intent = null;
try {
intent = new Intent(this,
Class.forName("ir.sibvas.testlibary1.HelloWorldActivity"));
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
First module activity launch then second module activity launch
and write a line of code is perfectly fine.
try {
Intent launchIntent =
getPackageManager().getLaunchIntentForPackage("com.your.packagename");
if (launchIntent != null) {
startActivity(
launchIntent); //null pointer check in case package name was not found ClassNotFoundException
}
} catch (e) {
e.printStackTrace();
}
Intent intent = new Intent();
intent.setClassName(context.getPackageName(), "ir.sibvas.testlibary1.HelloWorldActivity");
startActivity(intent);
I'm new in this world. I have a problem when I use startActivity(intent).
This is the Manifest:
<activity
android:name="com.example.counter.Splash"
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.counter.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
And this is the code:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run()
{
try
{
sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent i=new Intent ("com.example.counter.MainActivity");
startActivity(i);
}
}
};
timer.start();
}
I'd want to show Splash activity for 5 seconds and then show MainActivity.
LogErrors: !https://www.dropbox.com/s/kg7xyp6h4b95itq/Screenshot%202014-02-08%2016.57.36.png
There are two ways of doing what you are trying to do.
Using an implicit Intent
Using an explicit Intent
Refer Intent Types
Implicit Intent
Declare Intent Filters for your Activity in your AndroidManifest.xml. By doing that the Android system understands what kind of Intents your component(in this case your MainActivity) can handle.
<activity
android:name="com.example.counter.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.counter.MainAction" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<activity>
Now you will be able to launch your Activity with the same Intent
Intent i=new Intent ("com.example.counter.MainAction");
startActivity(i);
Such implicit Intents are used when you don't explicitly know which Activity has to be started and you want the Android system to decide which component to start. If the system finds multiple components which can handle your Intent, it will allow the user to choose.
Note: it is possible that there are no applications that can handle your intent. In this case, your application will crash when you invoke startActivity(). To avoid this, before calling startActivity() you should first verify that there is at least one application registered in the system that can handle the intent. To do this use resolveActivity() on your intent object.
Explicit Intent
In your case, you should use an explicit Intent as you already know which Activity you want to start. So create an Intent by passing the context and the component(Activity) class you want to start.
Intent i=new Intent (this,MainActivity.class);
startActivity(i);
You have to reference the class you want to start. So you'd need something like:
Intent newAct = new Intent(this, Splash.class);
startActivity(newAct);
What you're passing is an Action that is not understood as a class name.
I guess, Splash is your Launcher Activity, make following changes in your manifest file:
<activity
android:name="com.example.counter.Splash"
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.counter.MainActivity"
android:label="#string/app_name" >
</activity>
Make your activity this way:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
/*Splach screen that display for 5 seconds when app start*/
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
finish();
}
}, 5000);
}
}
I hope this should solve your problem now.
I think you should be able to use (implicit Intent):
Intent i=new Intent ("com.example.counter.MainActivity");
There is no reason to change it to (explicit intent):
startActivity(new Intent(mContext, MainActivity.class));
but then you need to change the action in intent filterof MainActivity from:
<action android:name="android.intent.action.MAIN" />
to:
<action android:name="com.example.counter.MainActivity"/>
You need to declare an activity in manifest file.
Like this:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FirstActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activty
android:name="com.example.counter.MainActivity"/>
</application>
Hope it helps.
i think it is better if you use Handler put this code at the splash Activity at the onCreate
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
}, 1500);
and if you want to open it one time it is good to use SharedPreferences
I'm trying to use another project I already have in my main project but when I call the activity from it, I get the ActivityNotFoundException: have you declared this activity in your androidManifest?. This is what i've done :
final Intent intent = new Intent();
ComponentName cName = new ComponentName("com.GCM.pushnotifications","com.GCM.pushnotifications.RegisterActivity");
intent.setComponent(cName);
startActivity(intent);
and in the manifest :
<activity android:name=".RegisterActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
What am i doing wrong? this is what I found on the net.
You just have to provide the activity class name as the second parameter of ComponentName
Change this
ComponentName cName = new ComponentName("com.GCM.pushnotifications","com.GCM.pushnotifications.RegisterActivity");
to
ComponentName cName = new ComponentName("com.GCM.pushnotifications","RegisterActivity");
I've been able to successfully implement a search widget experience to use a content provider that returns a cursor with row data. The custom search suggestions appear nicely in a list under the ActionBar search box as expected.
What I need to do is send the selected search suggestion to a custom activity (presumably in a bundle?) It seems so simple but I've not been able to figure it out.
Currently, this code will ask me what app I'd like to use to open the intent. I'd like to send the selected suggestion data to the "MainActivity" in the manifest listed further below.
Thanks in advance!
searchable.xml
<?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"
android:searchSuggestAuthority="com.myapp.SearchProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestThreshold="2"
android:searchMode="queryRewriteFromText" >
</searchable>
the search activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// Handle the normal search query case
android.util.Log.w("****", "in ACTION_SEARCH");
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
// Handle a suggestions click (because the suggestions all use ACTION_VIEW)
android.util.Log.w("****", "in ACTION_VIEW");
doView(intent);
}
}
private void doSearch(String query) {
android.util.Log.w("search query:", query);
}
private void doView(final Intent queryIntent) {
Uri uri = queryIntent.getData();
String action = queryIntent.getAction();
Intent i = new Intent(action);
i.setData(uri);
startActivity(i);
this.finish();
}
}
the search section of the manifest:
<activity
android:name="com.myapp.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.myapp.SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<provider android:authorities="com.myapp.SearchProvider"
android:name="com.myapp.SearchProvider" />
<meta-data android:name="android.app.default_searchable"
android:value="com.myapp.SearchableActivity" />
Phew, I figured it out.
In my content provider, I have to add a column in the Matrix cursor called: SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA
and put in the value of the suggestion.
In my search activity doView() method I can extract it with:
Bundle extras = queryIntent.getExtras();
String data = extras.getString(SearchManager.EXTRA_DATA_KEY);
android.util.Log.w("keySet =", extras.keySet().toString()); // this showed me the keys available
android.util.Log.w(SearchManager.EXTRA_DATA_KEY, data);
From here, I can pass that on to my custom activity/intent. There may be a better way, but this works!