After attempting to perform a search and failing, I am trying to launch an activity to display results from a search, but the app crashes when it gets to the point of the intent:
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Intent mapIntent = new Intent(SearchActivity.this, MapResults.class);
mapIntent.putExtra("query", query);
startActivity(mapIntent);
}
here is the class it is supposed to launch:
public class MapResults extends MapActivity implements OnGestureListener, OnDoubleTapListener {
/** Called when the activity is first created. */
public String query;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
query = intent.getStringExtra("query");
Toast.makeText(this, "The query: " + query, Toast.LENGTH_LONG).show();
}
#Override
public boolean isRouteDisplayed() {
return false;
}
and the manifest file:
<activity android:name=".SplashScreen"
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=".Main">
<intent-filter>
<action android:name="com.example.android.test.Main" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
<activity android:name=".SearchActivity"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
<meta-data android:name="android.app.searchable"
android:resource="#layout/searchable"/>
</activity>
<activity android:name=".MapResults">
<intent-filter>
<action android:name="com.example.android.test.MapResults"
android:label="#string/map_results_title"/>
</intent-filter>
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
I know that in the MapResults class it is not launching a map at the moment, its just displaying text, but this is just while I am trying to receive the data from the intent.
Any idea, cause I'm stuck!
In your MapResults class, the line :
Toast.makeText(this, "The query: " + query, Toast.LENGTH_LONG).show();
will create a NullPointerException as you are concatenating a null String. Your data member query is null at this point, you should have something like this :
query = intent.getStringExtra( "query" );
Toast.makeText(this, "The query: " + query, Toast.LENGTH_LONG).show();
Otherwise, your mechanism to transfer data using intents seems alright to me.
Regards,
Stéphane
The only problem, that I can see in the code is your intent variable is null, so it crashes on intent.getAction()
up: haven't noticed MapActivity code - if variable query is not initialized at the moment of Toast - yes, it's a second problem
<intent-filter>
<action android:name="com.example.android.test.Main" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
can you change DEFAULT to LAUNCHER?
just try it
Related
I am only new to android this is my first application.
I am making progress on an app that fires when the NFC reader detects a TAG that contains NDEF messages, specifically when a URL is detect matching my domain.
I would like to add a splashscreen that fires triggered by the Android INTENT of but then passes the original INTENT to the mainactivity for further processing, I have made a start but not sure how to marry up the manifest and code to do what I am after.
MANIFEST.XML
<activity
android:name=".SplashScreenActivity"
android:theme="#style/Theme.MyApp.SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" android:theme="#style/Theme.MyApp.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="<my custom domain>"/>
</intent-filter>
</activity>
Any help on how I should structure my Manifest to deal with this scenario, the SplashScreenActivity I could use the intent filters here to ensure that it is triggered, but then I create a new intent which means losing the NDEF extras.
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
finish();
}
}
I would like to be able to "just pass" the whole original INTENT to the mainactivity but I am unsure how to do this.
Here is the code that extracts the NDEF messages from the INTENT any ideas appreciated.
private void readFromIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
logViewModel.insert(new LogEntry("READFROMINTENT ACTUALLY FIRED", "Action " + intent.getExtras()));
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
getNdefRecords(msgs);
addLinkToDbFromRecord(records);
}
//buildTagViews(msgs);
}
}
So I managed to solve this by doing the following.
I moved my intent filters from MainActivity to the .splashscreen actvity changed the category of mainactivity to DEFAULT, and allowed splashscreen activity to remain the LAUNCHER
<activity
android:name=".MainActivity"
android:theme="#style/Theme..NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.myap.app.SplashScreenActivity"
android:theme="#style/Theme.myapp.SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="my domain"/>
</intent-filter>
</activity>
In my splashscreen activity i used the following code to copy the extras from the original intent, set the action to the expected action the mainactivity was designed to handle and used this to start the main activity
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent startMainActivity = new Intent(SplashScreenActivity.this, MainActivity.class);
startMainActivity.putExtras(getIntent());
startMainActivity.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
startActivity(startMainActivity);
finish();
}
}
I have an intent filter for receiving intents with a specific URI. When another app opens my app with that specific URI, I use that deeplink/unilink, and redirect in the app using a Flutter plugin, depending on which query parameters are included in the URI.
In AndroidManifest:
<activity android:name=".MainActivity" android:theme="#style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize" android:screenOrientation="portrait">
<meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="#drawable/launch_background" />
<meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="#style/NormalTheme"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="appspecifictheme" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="www.myappslandingpage.com"
android:pathPrefix="/myappspath" />
<data
android:scheme="https"
android:host="www.myappslandingpage.com"
android:pathPrefix="/myspecificpathprefix" />
</intent-filter>
</activity>
This is working fine when launching from another app with uri https://www.myappslandingpage.com/myspecificpathprefix/?param1=xxxx¶m2=xxxx¶m3=xxxx
Now to my issue: another app wants to launch my app using this intent from package manager, and passing the query parameters as extras instead:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("receiving.apps.package.name");
launchIntent.putExtra("param1", "xxxx");
launchIntent.putExtra("param2", "xxxx");
launchIntent.putExtra("param3", "xxxx");
startActivity(launchIntent);
Question 1: When I launch my app from another app using above code, and the app is opened, but how do I get the content from the intent? I have tried using getIntent() in my MainActivity's onCreate(), but it doesn't seem to be working (stringExtras and other data in the intent is null).
#Override
protected void onCreate(Bundle savedInstanceState) {
checkIntent(getIntent());
FlutterMain.startInitialization(this);
super.onCreate(savedInstanceState);
}
void checkIntent(Intent i) {
final String param1 = i.getStringExtra("param1");
final String param2 = i.getStringExtra("param2");
final String param3 = i.getStringExtra("param3");
final Bundle extras = i.getExtras();
if(extras != null) {
for (String key : extras.keySet()) {
L.d(TAG, "checkIntent() extra: " + key + " : " + (extras.get(key) !=
null ? extras.get(key) : "NULL"));
//Above logs nothing - there are no keys in extras.
}
}
if(param1 != null) {
//Do something
} else {
L.d(TAG,"checkIntent(): param1 is null");
}
}
I have also tried adding URI to the intent using launchIntent.setData("uri") before running startActivity() from the other app, and changing the intent filter in my receiving app as below, but getIntent().getData() in MainActivity also returns null.
<category android:name="android.intent.category.LAUNCHER" />
Question 2: If I find a way to receive the intent and get the stringExtras, is there a simple way to "transform" that intent so that it is interpreted the same way as if the app was opened with a deeplink?
I have added the following code to my manifest file:
<activity
android:name="com.example.123.scan"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.scan" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Every time I start my app.
I could not get the intent of NFC that I need to proceed.
public void onResume() {
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
String sss=getIntent().getAction();
Toast.makeText(this,sss, Toast.LENGTH_SHORT).show();
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(sss)) {
Toast.makeText(this, "NDEF", Toast.LENGTH_SHORT).show();
processIntent(getIntent());
}
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(sss)) {
Toast.makeText(this, "TAG", Toast.LENGTH_SHORT).show();
processIntent(getIntent());
}
if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(sss)) {
Toast.makeText(this, "TECH", Toast.LENGTH_SHORT).show();
processIntent(getIntent());
}
}
Every time, it toasted android.intent.action.scan instead of the NFC intent.
What is the problem?
I created an Android Library for easy NFC on android, check it out it might be an easy solution - link
I have followed the following tutorial : http://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app-natively-using-eclipse/
But even after editing android manifest xml as told there I am getting the following error:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.google.zxing.client.android.SCAN pkg=com.google.zxing.client.android (has extras) }
My Code :
public class BarCodeScannerActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ok;
ok=(Button) findViewById(R.id.b1);
ok.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("Helllllllloooooooo");
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE","QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
System.out.println("onActivityResult________resultCode________ "+resultCode);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
System.out.println("contentsssssssssssssssssssssss" + contents);
Toast.makeText(getApplicationContext(),"Congratulations!!!... Product Code"+ contents + "On Scanning This Item..." ,Toast.LENGTH_LONG).show();
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
System.out.println("Formaattttttttttttttt " + format);
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
And mainfest file:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".BarCodeScannerActivity"
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.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.CAMERA" />
Hi
Now i am getting a strange problem of attached screen shot once i updated my manifest as follows:
<activity android:name="com.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
![enter image description here][1]
<activity android:name=".ScanItemActivity"
android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
</activity>
I mean it says ""Sorry, the Android camera encountered a problem. You may need to
restart the device."
Nothing is there in logcat.
This is quite confused. You don't need to change your manifest at all if you are integrating by Intent, so remove anything you changed just for the integration.
The app is not installed, and you are not handling this properly. You must catch ActivityNotFoundException, or determine ahead of time that the app to handle the Intent is installed.
But, there is no need for any of this complexity. See http://code.google.com/p/zxing/wiki/ScanningViaIntent . You can use IntentIntegrator, which does all of this for you correctly, in a few lines of code.
I am trying to integrate an open source app into my android app. I have made the open source app as a library app and integrated the xml into my android manifest file as well. There are no compile errors.
First Screen is the login screen for the library app and when it is called it is throwing java lang class exception error at:
m_app = (TodoApplication) getApplication();
source code of loginscreen.java:
public class LoginScreen extends Activity {
final static String TAG = LoginScreen.class.getSimpleName();
private TodoApplication m_app;
private Button m_LoginButton;
private BroadcastReceiver m_broadcastReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
m_app = (TodoApplication) getApplication();
// supposed to help with the banding on the green background
findViewById(R.id.loginbackground).getBackground().setDither(true);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.todotxt.todotxttouch.ACTION_LOGIN");
m_broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, TodoTxtTouch.class);
startActivity(i);
finish();
}
};
registerReceiver(m_broadcastReceiver, intentFilter);
m_LoginButton = (Button) findViewById(R.id.login);
m_LoginButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
login();
}
});
//final RemoteClient remoteClient = m_app.getRemoteClientManager()
// .getRemoteClient();
//if (remoteClient.isAuthenticated()) {
switchToTodolist();
//}
}
private void switchToTodolist() {
Intent intent = new Intent(this, TodoTxtTouch.class);
startActivity(intent);
finish();
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(m_broadcastReceiver);
}
void login() {
final RemoteClient client = m_app.getRemoteClientManager()
.getRemoteClient();
if (!client.isAvailable()) {
Log.d(TAG, "Remote service " + client.getClass().getSimpleName()
+ " is not available; aborting login");
Util.showToastLong(m_app, R.string.toast_login_notconnected);
} else {
RemoteLoginTask loginTask = client.getLoginTask();
loginTask.showLoginDialog(this);
}
}
}
Integrated library code in android manifest.xml:
<activity android:name="com.todotxt.todotxttouch.LoginScreen" android:label="#string/app_label"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="com.todotxt.todotxttouch.category.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.todotxt.todotxttouch.TodoApplication" />
<activity android:name="com.todotxt.todotxttouch.Filter" android:label="Filter"
android:theme="#android:style/Theme.NoTitleBar" />
<activity android:name="com.todotxt.todotxttouch.Preferences" android:label="#string/set_preferences" />
<activity android:name="com.todotxt.todotxttouch.AddTask" android:label="#string/addtask"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity-alias android:name="com.todotxt.todotxttouch.AddTaskShortcut"
android:targetActivity="com.todotxt.todotxttouch.AddTask" android:label="#string/shortcut_addtask_name">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>
<activity-alias android:name="com.todotxt.todotxttouch.AddTaskShare"
android:targetActivity="com.todotxt.todotxttouch.AddTask" android:label="#string/share_addtask_name">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity-alias>
<activity android:name="com.todotxt.todotxttouch.HelpActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<activity android:name="com.todotxt.todotxttouch.TodoTxtTouch" android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Could anyone please help me on understanding the problem.
Let me explain further: I am having a file called Todoapplication.java....so the class exists...it being called from LoginScreen.java as
m_app = (TodoApplication) getApplication();
and that is where I am getting java lang class exception?
Activity.getApplication() returns an instance of the application class that was declared in the manifest in the <application> element. I don't see it in your pasted manifest.
It's not enough to simply have the application class in your app. It must be explicitly designated as one in the manifest.
I maybe getting the wrong end of the stick so I beg the programming gods for forgiveness in advance.
Assuming you are developing in Eclipse, is this not a simple case of having a project in Eclipse with the open source source in, which within the project properties has the option isLibrary ticked.
In YOUR project's properties you can add a library and Eclipse will list the open source one (and any others that have the "isLibrary" checked). Would you not simply select the open source project and add it. Your project will then add the library and build again?
To access the open source project, now a library, you can use "import" statements to access any public methods that are exposed.
A good example of this setup process using an open source library project is Actionbar Sherlock to which I wrote a tutorial youtube that demonstrates visually what I have just written. It can be found at http://www.youtube.com/watch?v=avcp6eD_X2k