Need to run my app before the unlock screen in android - android

I need to run my app as the user clicks on the unlock screen. I have used ACTION_USER_PRESENT Intent in a BroadcastReceiver to check. I used the following code. I need to show my app before the unlock screen. But my app is visible after I unlock the screen. Any help will be appreciated.
My BroadcastReceiver
package com.progressindicator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.WindowManager;
public class Receive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction() != null) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent s = new Intent(context, ProgressIndicator.class);
s.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
// s.setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
s.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(s);
}
}
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.progressindicator"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.progressindicator.ProgressIndicator"
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=".Receive" >
<intent-filter
android:enabled="true"
android:exported="false" >
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
</application>
</manifest>
My Activity:
package com.progressindicator;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ProgressIndicator extends Activity {
ImageView loading;
AnimationDrawable loadAnimation;
private Window wind;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_progress_indicator);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// requestWindowFeature(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
// wind = this.getWindow();
// wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
// wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
// wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
);
LinearLayout main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
setContentView(main);
loading = new ImageView(this);
loading.setImageResource(R.drawable.loading);
loadAnimation = (AnimationDrawable)loading.getDrawable();
main.addView(loading);
}
#Override
public void onWindowFocusChanged(boolean hasFocus){
loadAnimation.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.progress_indicator, menu);
return true;
}
}

Try adding WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED for your activity to be started, this will temporarily disable keyguard/keylock as long as your activity is on top of all other windows.

I used this for a project some time ago
// set flags for staying on and lockscreen
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
Also I would check your Intent parameters to make sure your creating a new Activity and onCreate is called.
If you want to disable the lock screen, you will need to be a Device Administrator.
EDIT:
ACTION_USER_PRESENT is sent after the user unlocks the screen, that's why it doesn't works for showing a screen on top of the lockscreen.
You should change to ACTION_SCREEN_ON.
Edit 2:
On the manifest I have android:launchMode="singleTask" and in the intent I call the activity with:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

You need to add WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED to your
window flags, as in Activity.getWindow().addFlags(...).
I recommend to use this in addition with FLAG_WATCH_OUTSIDE_TOUCH and call
it in your onCreate lifecycle method.
To the "security hole" topic. It is correct that as an app developer you are
able to pop up before the security lockscreen appears, but a simple
home button tap will pause your activity (even a System-Alert) and show the
secure guard.
I know that there are some hacky ways around this, but they do not work
with every device, are somewhat broken on HTC and Samsumg and no app should
be able to lock out the user.
Steve

Related

Activity class MainActivity doesn't exist . I dont know why?

When I run my code in my phone, It is not running. I'm worried about it.Can't figure out what the problem is.I have 5 java files(I don't know if I can call it as activity) MainActivity.java, MainActivity2.java, Person.java, DatabaseHandler.java, GetDetails.java. and activity_main.xml, front_page.xml, refer_mail.xml
Its a lengthy post. sorry for that.
This is my console message.
[2015-08-28 19:23:58 - FormDetails] ActivityManager: Error type 3
[2015-08-28 19:23:58 - FormDetails] ActivityManager: Error: Activity class {com.Prasad.formdetails/com.Prasad.formdetails.MainActivity} does not exist.
This is my MainActivity.java File.
package com.Prasad.formdetails;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button register=(Button)findViewById(R.id.register);
Button details=(Button)findViewById(R.id.get_details);
//Register Functionality
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
});
//Get Details Functionality
details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this, GetDetails.class);
startActivity(intent);
}
});
}
}
This is my AndroidManifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Prasad.formdetails"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:enabled="false"
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>
<activity android:name=".MainActivity2" />
<activity android:name=".GetDetails" />
</application>
</manifest>
Remove android:enabled="false" from Your Application Tag. Then Clean,Rebuild And Restart Your Project .Try this way .I hope it will helps you
android:enabled
Whether or not the Android system can instantiate components of the application — "true" if it can, and "false" if not. If the value is "true", each component's enabled attribute determines whether that component is enabled or not. If the value is "false", it overrides the component-specific values; all components are disabled.
The default value is "true".
package="com.Prasad.formdetails"
you should not use capital letters in package name . Use package="com.prasad.formdetails"
I'd say it's your android:enabled="false" in the application tag. Your package is disabled, hence your activity is disabled, too.

Unfortunately [yourapp], has stopped

I'm using intent method to make a simple app that takes you from main screen [with an enter button] to another screen which has three options. Code sourced online and seems to be error free, though my app crashes saying "Unfortunately [yourapp], has stopped" immediately after i press the button which is meant to take to the the other screen.
This is my first activity code:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.enterBtn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, HomeActivity.class);
startActivity(intent);
}
});
}
}`
And this is my landing screen's activity code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class HomeActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
I'm really stuck with this issue and any help would be much appreciated. Many thanks in advance.
Every activity needs to be added in your manifest file under application tag. This seems to be the problem in your case.
Try to post you LogCat so that we might get some more information and if you have not yet added your Activity in the manifest, this is the way of adding it (Activities go under application tag)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.southmp3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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>
<activity
android:name=".HomeActivity"
android:label="#string/app_name" >
</activity>
</application>
all the Activities should be going under the application tag
I had forgotten to register my new activity on the androidmanifest.xml file. That's what sorted my app crash issue.

Creating an action on notification being pressed, Android

So cant seem to get this configured or running properly.
Trying to get the notification to open the MainActivity3 file and then to have it run whatever I want, at the minute just aiming for it to play a sound.
Completely new to android development but think Im along the right lines.
You will see I have two approaches but cant get either to work.
MainActivity.java
package com.example.firstapp;
import android.media.MediaPlayer;
//import android.media.RingtoneManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
//import android.app.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//vibrate controls
//Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.follownavi)
.setContentTitle("Hey")
.setContentText("");
PendingIntent pendingIntent;
//Intent intent = new Intent();
Intent intent= new Intent(MainActivity3.onNewIntent(), MainActivity3.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
//intent.setClass(getApplicationContext(),MainActivity3.class);
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
mBuilder.addAction(R.drawable.follownavi,"LISTEN",pendingIntent);
mBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
//get touch location
int x = (int)event.getX();
int y = (int)event.getY();
//put on screen
TextView tester = (TextView) findViewById(R.id.textView1);
tester.setText("this is x:" + x + " and this is y:" + y);
//move image
ImageView iv = (ImageView) findViewById(R.id.NaviFollow);
if(y > 222)
{
iv.setX(x - 125);
iv.setY(y - 350);
//cause vibration
Vibrator v = (Vibrator) getSystemService(VIBRATOR_SERVICE);
v.vibrate(300);
}
return true;
}
}
Once the notification is called and appears its meant to run this file
MainActivity3
package com.example.firstapp;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
public class MainActivity3 extends Activity {
protected Context onNewIntent()
{
MediaPlayer mp2 = MediaPlayer.create(getApplicationContext(), R.raw.listen);
mp2.start();
return null;
}
}
The error seems to be that the call is static but it wants void, very confused
Error code
Cannot make a static reference to the non-static method onNewIntent() from the type MainActivity3 MainActivity.java /firstApp/src/com/example/firstapp line 38
and if I change the method to fit then the mediaplayer getApplicationContext wont work
after doing more research I feel the manifest might be stopping it but still unsure to the structure of android.
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/navibutton"
android:label="Navi"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.firstapp.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>
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
Please help as getting really boged down and tempting to give up
You are not doing anything with PendingIntent. You should set it in Notification Builder before calling build()
mBuilder.setContentIntent(pendingIntent);
Also you must be aware that creating a new Activity and doing something on onCreate is not a good idea for executing an operation. If you want, for example to show a toast in the same activity you can set specific data in Intent and do it in onNewIntent().
The accepted answer has an example for configuring handling of new intent.
Android: new Intent() starts new instance with android:launchMode="singleTop"

Android Application Crashing

I'm trying to make my application start another class.
What im trying to learn is how to get another class to run in the background - like if the user opens the application, the application stays running.
I thought if I could try to open another class by using an intent, it would work. When i run my application on the emulator, it just crashes...
Here is the opening:
package omg.justry;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
//super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
public void onCreate(Bundle savedInstanceState) {
Intent openStartingPoint = new Intent("omg.justtry.PartF**king2");
startActivity(openStartingPoint);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
Here is the "PartF**king2" class:
package omg.justry;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
public class PartF**king2 extends Activity{
public void onCreate(Bundle savedInstanceState) {
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
The thing is, Eclipse doesn't show any errors. I just exported the app and installed it to the emulator using adb.
I also added the class to the AndroidManifest as you see here:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="omg.justry"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="PartF**king2"></activity>
</application>
I think its the manifest now that I look at it but whatever i do, it gets an error or crashes with Eclipse not explaining anything.
In every class, you onCreate(Bundle savedInstanceState) method MUST contain
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
The super is absolutly mandatory, and the setContentView defines the layout for your activity.
And an Activity cannot "run in the background". Start by reading some Android tutorial, and you'll have some clues about what to do.

New intent, how to link to my class?

I'm beginner in Android (and java), and I'm just playing around and want to create new View after clicking a button. Here is my code so far:
Main class
package myTests.homeSpace;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener( new OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(TestsActivity.this, screen1.class);
startActivity(myIntent);
}
});
}
}
Class to run after clicking button
package myTests.homeSpace;
import android.app.Activity;
import android.os.Bundle;
public class screen1 extends Activity {
#Override
public void onCreate(Bundle SavedBundleInstance)
{
super.onCreate(SavedBundleInstance);
setContentView(R.layout.screen1);
}
}
The problem is, after hitting button I got "Unforunately, Tests has stopped" error. ("Tests" is application name). I know (or guess) the problem is in this line Intent myIntent = new Intent(TestsActivity.this, screen1.class);
I guess my reference to class screen1 is wrong somehow, but I have no idea why. There are no compilation errors nor warnings, layout .xmls shouldn't be wrong.
Could any of you please advice me any solution?
EDIT
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myTests.homeSpace"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestsActivity"
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>
</manifest>
CONSOLE OUTPUT
[2012-06-20 19:56:21 - ddms] null
java.lang.NullPointerException
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:575)
at com.android.ddmlib.HandleHello.sendHELO(HandleHello.java:142)
at com.android.ddmlib.HandleHello.sendHelloCommands(HandleHello.java:65)
at com.android.ddmlib.Client.getJdwpPacket(Client.java:672)
at com.android.ddmlib.MonitorThread.processClientActivity(MonitorThread.java:317)
at com.android.ddmlib.MonitorThread.run(MonitorThread.java:263)
I bet if you read the crash log somewhere in there says did you forget to declare this activity in the Manifest?
I agree, not declaring the activity in your Manifest file (AndroidManifest.xml) is most likely your problem. See the android developer page on activities for more information. Here is the relevant description:
Declares an activity (an Activity subclass) that implements part of
the application's visual user interface. All activities must be
represented by elements in the manifest file. Any that are
not declared there will not be seen by the system and will never be
run.
I just posted this so you would have more space to research on your own.
Declare your second activity in the Manifest.xml as:
<activity
android:name=".screen1">
</activity>
Just below the declaration of your first activity.

Categories

Resources