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");
}
Related
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" />
I have written a simple broadcast receiver responding to TIME_TICK action .
When I add the action in the manifest file it is not calling the registered receiver but when I register the receiver in the java code it is being called.
I have a simple onreceive method.
public class mybroad extends BroadcastReceiver
{
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Log.v("got", "broadcasted");
Toast.makeText(arg0, "hurray broadcast got", Toast.LENGTH_LONG).show();
}
}
receiver tag for manifest file
<receiver android:name="com.example.chapbasic.mybroad" >
<intent-filter>
<action android:name="android.intent.action.TIME_TICK"></action>
</intent-filter>
</receiver>
when I operate with the following code it is working
public class broadact extends Activity
{
IntentFilter ii;
mybroad mb;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mybroad);
ii=new IntentFilter("android.intent.action.TIME_TICK");
mb=new mybroad();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
registerReceiver(mb, ii);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(mb);
}
Kindly update why it is not being called from the manifest file registration.
thanks
kindly go through the documentation that states.
You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver() .
That is why you are not able to receive it when doing through manifest file.
thanks
How can I make service listen for media button (headset-hook) in android device .
I try to do that using this code but did not work.
Can you help me.
Service code:
public class RLservice extends IntentService {
private Handler handler;
public RLservice() {
super("my service");
// TODO Auto-generated constructor stub
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler = new Handler();
return super.onStartCommand(intent, flags, startId);
}
#Override
protected void onHandleIntent(Intent arg0) {
String intentAction = arg0.getAction();
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
KeyEvent event = (KeyEvent) arg0
.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int keycode = event.getKeyCode();
int action = event.getAction();
long eventtime = event.getEventTime();
if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
|| keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
if (action == KeyEvent.ACTION_DOWN) {
// Start your app here!
handler.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Welcome",
Toast.LENGTH_LONG).show();
}
});
}
}
}
}
}
This is reciver code:
public class RLreciver extends BroadcastReceiver
{
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Intent i=new Intent(RLservice.class.getName());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startService(i);
}
}
This is manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ypu.RLservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".RLreciver" >
<intent-filter android:priority="100000" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<service
android:name=".RLservice"
android:process=":remote" >
<intent-filter>
<action android:name="com.ypu.RLservice.RLservice" />
</intent-filter>
</service>
</application>
</manifest>
Thank you in advance.
Your service is expecting an Intent with the ACTION_MEDIA_BUTTON Intent action. You are not starting the service using such an Intent.
Change:
Intent i=new Intent(RLservice.class.getName());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startService(i);
to:
arg1.setClass(this, RLservice.class);
arg0.startService(arg1);
This will effectively "redirect" the broadcast as a command to your service, with the action and extras intact.
Also:
Get rid of android:process=":remote", as it is not necessary here and is user-hostile
Get rid of your <intent-filter> in your <service>, unless you intend for a million other apps to be calling this service
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>
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);