Android: Search Intent not working - android

I seem to be running into a number of problems getting my app going ... grrr
The issue I have at the moment is, I have an Activity on launch of my app that is a searchable facility (via android quick search). The search function works and launches my SearchActivity class where the results as displayed, simples.
BUT, when I attempt a search from the results activity (SearchActivity), the search box pops up and accepts in put, but the intent is never received by SearchActivity or it never launches SearchActivity ... I'm not sure.
Here is the SearchActivity class (this uses the same OnClickListener as the working class):
public class SearchActivity extends MapActivity implements OnGestureListener, OnDoubleTapListener {
public boolean touched = false;
private MapView mapView;
private MapController mapController;
private PlacesItemizedOverlay placesItemizedOverlay;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
Button searchRequest = (Button)findViewById(R.id.searchRequest);
searchRequest.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onSearchRequested();
}
});
mapView.getController();
handleIntent(getIntent());
}
#Override
public boolean onSearchRequested() {
return super.onSearchRequested();
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// handles a search query
String query = intent.getStringExtra(SearchManager.QUERY);
mapFromQuery(query);
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onDoubleTap(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
The list of taps I don't believe are affecting anything, but I included them just incase. Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.android.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<uses-library android:name="com.google.android.maps" />
<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.test.android.example.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.searchable"
android:resource="#layout/searchable"/>
</activity>
</application>
</manifest>
Please help :(

Finally found the solution!!
All that needed to be added was:
#Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntent(intent);
}
Thats it! Bet someone else is looking for the same result, enjoy!:)

Why not use normal intents with extras? Like so:
Intent intent = new Intent();
intent.setClassName("yourpackage","yourpackage.youractivity");
intent.putExtra("Var name", yourVar); //yourVar can be any type, int, string, etc...
startActivity(intent);

Related

Why websocket connection closes immediately...?

When i tried to create a websocket connection in android for implementing wamp using Autobahn, the connection is created and closed immediatley.
why this happening ....?
How to solve..........?
Part of my code is given below
private void start() {
// TODO Auto-generated method stub
final String wsuri = "ws://192.168.0.102:8080/ws";
if (!connection.isConnected()) {
Log.d("tag", "Not Connected");
}
connection.connect(wsuri, new ConnectionHandler() {
#Override
public void onOpen() {
// TODO Auto-generated method stub
Log.d("tag", "Connected to " + wsuri);
testPubSub();
}
#Override
public void onClose(int code, String reason) {
// TODO Auto-generated method stub
Log.d("tag", "disconnected");
}
});
}
protected void testPubSub() {
// TODO Auto-generated method stub
connection.subscribe(TOPIC, String.class, new EventHandler() {
#Override
public void onEvent(String topicUri, Object event) {
// TODO Auto-generated method stub
Log.d("tag", "Event Recieved");
}
});
}
Sorry for not posting the manifest file earlier. Its given below..
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testwamp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<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>
</application>
Most probably you didn't put INTERNET permission to the manifest.xml
<uses-permission android:name="android.permission.INTERNET" />

How to set a different java file as launcher

I am new to android and i have created a class in my android project other than the default MainActivity class and i want to start my project with that file. Here is what i added to my manifest file:
<activity
android:name="com.example.surfaceview.SurfaceViewExample"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SURFACEVIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And the class that i created:
public class SurfaceViewExample extends Activity implements OnTouchListener {
OurView v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v=new OurView(this);
setContentView(v);
v.setOnTouchListener(this);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOK=false;
public OurView(Context context) {
super(context);
holder=getHolder();
}
#Override
public void run() {
while(isItOK){
if(holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(255, 155, 155, 10);//canvas backgroundu boyama
holder.unlockCanvasAndPost(c);
}
}
public void pause(){ //pause the thread
isItOK=false;
while(true){
try{
t.join();
}catch(InterruptedException e){
e.printStackTrace();
}
break;
}
t=null;
}
public void resume(){ //resume the thread
isItOK=true;
t=new Thread(this); //this parameter means use this run method
//which is inside that class
t.start();
}
}
#Override
public boolean onTouch(View v, MotionEvent me) {
return false;
}
}
But the application does not start. I think the problem might be in that line inside the intent filter:
<action android:name="android.intent.action.SURFACEVIEW" />
Can anyone help me with this?
Thanks
Update your manifest with the following code for your activity. This should now launch your activity.
<activity
android:name="com.example.surfaceview.SurfaceViewExample"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I hope this helps.

Broad Cast Manager issue

I am a new in Android programming.I create one alarm manager and Broadcast manager program . The Broadcast manager class is defined as inner class in main activity. But the inner class is not working. I also give the code here. Please help me.
public class Alarmactivity extends Activity {
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startAlert(null);
}
});
}
public void startAlert(View view) {
AlarmManager am=(AlarmManager)this.getSystemService(this.ALARM_SERVICE);
Intent i = new Intent(this, Broadcas.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0,i,0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 15, pi);
}
#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 class Broadcas extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("**********************hello***********************************");
}
}
}
Have you registered the receiver with proper name in the manifest file? Simply giving the BroadcastReceiver name won't work in this case and mostly you need to use the name
Alarmactivity$Broadcas
EDIT:
This is my activity class:
public class MainTestActivity extends Activity {
private Button send_broadcast = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
send_broadcast = (Button) findViewById(R.id.send_broadcast);
send_broadcast.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startAlert(null);
}
});
}
#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_test, menu);
return true;
}
public void startAlert(View view) {
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, Broadcas.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 15, pi);
}
public static class Broadcas extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("**********************hello***********************************");
}
}
}
And this is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.coding.test"
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.coding.test.MainTestActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MainTestActivity$Broadcas"></receiver>
</application>
</manifest>

Starting intentservice over adb fails

When I try to start an IntentService over adb i get tho following error
E/AndroidRuntime( 2418): java.lang.RuntimeException: Unable to instantiate service
com.myCompany.MyPackage.Service: java.lang.InstantiationException:
com.myCompany.MyPackage.Service
when I start a normal service ovre adb, everithing works fine.
What are the reason a cannot start an intentservice over adb?
public class NCService extends IntentService {
public NCService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
NetworkStateReceiver nsr;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "onStartCommand", 0).show();
nsr = new NetworkStateReceiver();
return START_STICKY;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "onCreate", 0).show();
super.onCreate();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "onDestroy", 0).show();
super.onDestroy();
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
}
}
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<service android:name=".NCService" />
<receiver android:name=".NetworkStateReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
</application>
Regards
Simon
Your service need a default no-arg constructor, otherwise Android does not know what argument pass as name parameter:
public NCService() {
super("MyNCService");
}

I'm getting ClassNotFound exception in BroadcastReceiver

I'm trying to use this very simple Service and BroadcastReceiver but I'm getting a
ClassNotFound exception. If I don't use startService things are fine so the problem it's with the receiver. I have registered it in the android manifest file so why do I get the ClassNotFound exception?
I will be using this method of communication for polling a php file and updating a ListView. Is this the appropriate way of communicating (broadcast intent)?
public class MessengerServiceActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent(this,MessengerService.class));
}
class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle b=intent.getExtras();
if(b==null)
return;
String msg=b.getString("message");
TextView tv=(TextView) findViewById(R.id.textView1);
tv.setText(msg);
}
}
}
public class MessengerService extends Service {
HttpClient hc;
HttpPost hp;
String s[]={"pratik","harsha","dayal","hritika"};
public MessengerService() {
// TODO Auto-generated constructor stub
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
hc=new DefaultHttpClient();
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Thread t=new Thread(new Runnable(){
public void run()
{
int k=0;
while(true)
{
Intent i=new Intent("com.pdd.messenger.MyAction");
i.putExtra("message",s[k]);
sendBroadcast(i);
try
{
Thread.sleep(3000);
}
catch (InterruptedException e)
{
Toast.makeText(getApplicationContext(), e.getLocalizedMessage(), Toast.LENGTH_LONG)
.show();
}
k=(k+1)%4;
}
}
});
t.start();
return super.onStartCommand(intent, flags, startId);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pdd.messenger"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MessengerServiceActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver" android:enabled="true">
<intent-filter>
<action android:name="com.pdd.messenger.MyAction"/>
</intent-filter>
</receiver>
<service android:name=".MessengerService"></service>
</application>
</manifest>

Categories

Resources