android - Start new Activity On Sensor Changed? -
how can start new activity on accelererometer (on shake): when shake phone app crashes - accelerometer run in background
public class shaker_service extends service implements sensoreventlistener{ private static final string tag = "myservice"; private sensormanager sensormanager; apppreferences appprefs; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { toast.maketext(this, "my service created", toast.length_long).show(); log.d(tag, "oncreate"); } @override public void ondestroy() { toast.maketext(this, "my service stop", toast.length_long).show(); log.d(tag, "ondestroy"); } @override public void onstart(intent intent, int startid) { toast.maketext(this, "my service start", toast.length_long).show(); log.d(tag, "onstart"); sensormanager=(sensormanager)getsystemservice(sensor_service); // add listener. listener helloandroid (this) class sensormanager.registerlistener(this, sensormanager.getdefaultsensor(sensor.type_accelerometer), sensormanager.sensor_delay_normal); } @override public void onaccuracychanged(sensor sensor, int accuracy) { } @override public void onsensorchanged(sensorevent event) { // check sensor type if(event.sensor.gettype()==sensor.type_accelerometer){ // assign directions float x=event.values[0]; float y=event.values[1]; float z=event.values[2]; if (x>10){ startactivity(newintent("com.examles.message")); } } } }
manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.examples" android:versioncode="1" android:versionname="1.0"> <activity android:name=".message_note" android:label="@string/app_name" > <intent-filter> <action android:name="com.examples.message" /> <category android:name="android.intent.category.default" /> </intent-filter> </activity> <service android:enabled="true" android:name=".shaker_service" /> </application> </manifest>
message_note.java :
public class message_note extends activity {
@override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.message); }
}
image of error (logcat) https://mega.co.nz/#!suptabac!wc9y_xlh5gew9ay9_5wbpxwkya4xk-o9wgaxvn6jplk
try using:
intent intent = new intent(this, theactivityyouwanttostart.class); intent.setflags(intent.flag_activity_new_task); startactivity(intent);
correct way start activity inside service.
Comments
Post a Comment