This is my code for implementation of OnPrimaryCLipChangedListener:
public class PrimaryClipChangedListener implements OnPrimaryClipChangedListener {
#Override
public void onPrimaryClipChanged() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Log.d("RAJATH", "copyclip reached");
}
}
My service which register the listener:
package com.example.tryservice;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.content.ClipboardManager.OnPrimaryClipChangedListener;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
#SuppressLint("NewApi")
public class MyService extends Service{
public MyService() {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d("RAJATH", "Service Reached");
ClipboardManager cb = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
cb.addPrimaryClipChangedListener(new PrimaryClipChangedListener());
return 0;
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
I have an activity that starts this service. The purpose of this code is to listen to clipboard changes in the background. Where is the mistake?
What exactly doesn't work? There is a bug in Android 4.3 where the system crashes if you listen for OnPrimaryClipChangedListener callbacks.
Related
Main.java
Package com.first.service;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Main extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(this,Myservice.class);
startService(i);
}
#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;
}
#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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MyService.java
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.TextView;
public class Myservice extends Service{
public Myservice(){
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.i(TAG, "Service Started");
return Service.START_STICKY;
}
}
I am new to android services and I want to create a new Service application, but logcat isn't displaying anything.
Please help me and suggest me, if there is anything wrong with the code.
I am new to StackOverflow.
See the image to have clear idea:
According to this:
Support for the Android Developer Tools (ADT) in Eclipse has ended. You should migrate your app development projects to Android Studio as soon as possible.
I just finished up publishing an app to the Google Play store, but when I downloaded it from the store on another phone, it didn't run at all. I set up my libgdx app project based off of this website:
http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-create-a-working-prototype-in-a-day-tutorial-part-1/comment-page-2/
which basically had me type all my game code in a Java Project, then launch that from an Android Application Project.
Here is my code for the Android Application Project. Any ideas why this wouldn't launch from the Google Play Store? I'm guessing because the APK does not include the Java Project code, but how do I make it include that?
package ball.activity;
import greenball.activity.GreenBall;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.backends.android.AndroidApplication;
import activity.ball.greenball_android.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;
public class GreenBallActivity extends AndroidApplication implements ApplicationListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_green_ball);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
initialize(new GreenBall(), cfg);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.green_ball, menu);
return true;
}
#Override
public void create() {
// TODO Auto-generated method stub
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void render() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
}
It sounds like there may be a problem with how your app is signed. It would have worked on your test device with just a debug key, but not on any other device. Android requires all apps to be signed before it will install them.
I'm working on an android map and i'm trying to use Service class to find the current location of users. I had already succeed to do that whitout Service (directly in the main activity) but given the fact that i'll do network operations to find adresses and points of interest i thinked it was a good training to try Service with current location.
But the problem is my service doesn't start at all even if i reported it in the manifest
my class which extends Service :
`
package toutel.testcarte;
import org.osmdroid.bonuspack.overlays.Marker;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
public class FindMe extends Service {
private MapView mapView;
private MapController mapController;
private LocationManager lm;
private LocationListener mylocationlistener = new LocationListener() {
public void onLocationChanged(Location location) {
// Context ctx = getApplicationContext();
GeoPoint myposition = new GeoPoint(location.getLatitude(),
location.getLongitude());
mapController.animateTo(myposition);
mapController.setCenter(myposition);
mapController.setZoom(17);
Marker marker = new Marker(mapView);
marker.setPosition(myposition);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
mapView.getOverlays().add(marker);
mapView.invalidate();
try {
Thread.sleep(5 * 1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
mapView.getOverlays().clear();
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
public IBinder onBind(Intent arg0) {
return null;
}
public void onCreate(Bundle savedInstanceState) {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0,
mylocationlistener);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0,
mylocationlistener);
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
super.onDestroy();
lm.removeUpdates(mylocationlistener);
}
}
`
Im starting the service in the main activity with
startService(new Intent(MainActivity.this, FindMe.class));
and i added the service in the manifest :
<service android:name = "toutel.testcarte.FindMe"> </service>
So what am i doing wrong ?
Thanks !
i am developing a android app that makes a phone webserver it works fine when i use it without the service but i want to use service so that my webserver runs in background this is my code please have a look at it..
Serivice runner class:
package dolphin.developers.com;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import dolphin.devlopers.com.R;
public class web extends Activity implements OnClickListener {
public static final int progress_bar_type1 = 0;
private static final String TAG = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.hotmail);
Button btnShowProgressa;
Button buttonStop = (Button) findViewById(R.id.button1ad);
buttonStop.setOnClickListener(this);
btnShowProgressa = (Button) findViewById(R.id.button2);
btnShowProgressa.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button2:
Log.d(TAG, "onClick: starting srvice");
startService(new Intent(this, webser.class));
break;
case R.id.button1ad:
Log.d(TAG, "onClick: stopping srvice");
stopService(new Intent(this, webser.class));
break;
}
}
}
Webserver service:
package dolphin.developers.com;
import java.io.File;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class webser extends Service {
Thread hi;
private static final String TAG = null;
JHTTP pro;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
hi= new Thread(){
public void run(){
try{
File documentRootDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/");
pro = new JHTTP(documentRootDirectory, 1234);
}
catch(IOException e ){
e.printStackTrace();
}
}
}; hi.start();
}
#SuppressWarnings("deprecation")
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
readysteadypo.stop();
pro.stop();
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
pro.start();
}
}
Manifest file:
<Service
android:name="dolphin.developers.com.webser"/>
Logcat:
08-05 13:04:12.221: W/ActivityManager(73): Unable to start service Intent { cmp=dolphin.devlopers.com/dolphin.developers.com.hot1 }: not found
The Problem is solved now. I added intent filter action and it is working fine.
Here goes the code:
<service android:name="dolphin.developers.com.webser" android:enabled="true">
<intent-filter>
<action android:name="dolphin.developers.com.webserv"></action>
</intent-filter>
</service>
here is the code of the class which i created which extends MainActivity and how can i call this from MainActivity?
I'm trying to figure out where I went wrong on referencing my surface view class, not my view. I only did the view as an example. Here is my main class:
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class SurfaceViewExample extends Activity implements OnTouchListener{
OurView v;
Bitmap ball;
float x,y;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
v=new OurView(this);
v.setOnTouchListener(this);
ball=BitmapFactory.decodeResource(getResources(),R.drawable.tennis_ball);
x = y = 0;
setContentView(v);
}
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable{
Thread t;
SurfaceHolder holder;
boolean isItOk=false;
public OurView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder=getHolder();
}
public void run() {
// TODO Auto-generated method stub
while( isItOk ==true)
{
//drawing
if(holder.getSurface().isValid()) {
continue;
}
Canvas c=holder.lockCanvas();
c.drawARGB(255,150,150,10);
c.drawBitmap(ball, x+(ball.getWidth()/4), y+(ball.getHeight()), null);
holder.unlockCanvasAndPost(c);
}
}
public void pause()
{
isItOk=false;
while(true) {
try {
t.join();
}catch(InterruptedException e) {
e.printStackTrace();
}
break;
}
}
public void resume()
{
isItOk=true;
t=new Thread(this);
t.start();
}
}
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}
The much i got is that you want to go SurfaceViewExample from your main activity for that you need to use intent on button click like
Intent i = new Intent(this, SurfaceViewExample.class);
startActivity(i) ;
and you have to add a permission in menifest to got to that activity like
<activity android:enabled="true" android:name="SurfaceViewExample" />
and you can see these links also
Calling one Activity from another in Android
http://developer.android.com/reference/android/content/Intent.html