deviceManger.lockNow() is Not Working on some Device - android

I am working on Phone Lock from this Reference. Working well on my Samsung Tab 2. But when I run the Same Code On Nexus 2. It does not work .I have posted some Code below.
It Logs "DeviceAdminSample" "Admin enable FAILED!". Any help will be Appreciated !!
Is there any Restriction on Nexus?
onCreate()
{
deviceManger = (DevicePolicyManager)getSystemService(
Context.DEVICE_POLICY_SERVICE);
activityManager = (ActivityManager)getSystemService(
Context.ACTIVITY_SERVICE);
compName = new ComponentName(this, MyAdmin.class);
}
private void EnableSetting() {
// TODO Auto-generated method stub
Intent intent = new Intent(DevicePolicyManager
.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
compName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"Additional text explaining why this needs to be added.");
startActivityForResult(intent, RESULT_ENABLE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RESULT_ENABLE:
if (resultCode == Activity.RESULT_OK) {
Log.i("DeviceAdminSample", "Admin enabled!");
} else {
Log.i("DeviceAdminSample", "Admin enable FAILED!");
}
}
Thanks.

Code for main Activity
public class LockerTest extends Activity {
protected static final int REQUEST_ENABLE = 0;
DevicePolicyManager devicePolicyManager;
ComponentName adminComponent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(btnListener);
}
Button.OnClickListener btnListener = new Button.OnClickListener() {
public void onClick(View v) {
adminComponent = new ComponentName(LockerTest.this, Darclass.class);
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!devicePolicyManager.isAdminActive(adminComponent)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
startActivityForResult(intent, REQUEST_ENABLE);
} else {
devicePolicyManager.lockNow();
}
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (REQUEST_ENABLE == requestCode) {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Create a new class - Darclass - code
import android.app.admin.DeviceAdminReceiver;
public class Darclass extends DeviceAdminReceiver{
}
Create a folder 'xml' in 'res'. Then create my_admin.xml file in 'xml' folder. Code for my_admin.xml. Note add this receiver after and before
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
</uses-policies>
</device-admin>
Finally add the receiver given bellow to your AndroidManifest.xml
<receiver
android:name=".Darclass"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/my_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
It should work on your device.

I had this same problem. It was because my ... block was outside of my block in AndroidManifest.xml. Worked perfectly when I moved the ... block, as shown here:
<activity
android:name=".Main"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyDeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin" android:resource="#xml/device_admin_xml" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>

Related

launch activity after phone restart or manually turn power off or on

how to launch an launch activity after phone restart or manually turn power off or on
i tried below codes but its working for only for unlocking phone but i also want to make it done for boot up manually or restart phone.
below is code
application.class
public class MyApplication extends Application {
public static SharedPreferences preferences;
public static SharedPreferences.Editor editor;
public static String PASSWORD = "password";
public static String IS_SET = "nothing";
public static String MYPREF = "mypref";
public String TAG = getClass().getName();
#Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
MultiDex.install(this);
}
#Override
public void onCreate() {
super.onCreate();
init();
}
public void init() {
preferences = getSharedPreferences(MYPREF, 0);
editor = preferences.edit();
startService(new Intent(getBaseContext(), ScreenReceiver.class));
}
}
ScreenReceiver.class
public class ScreenReceiver extends BroadcastReceiver {
SharedPreferences preferences;
#Override
public void onReceive(Context context, Intent intent) {
System.out.println(intent.getAction());
preferences = context.getSharedPreferences(MYPREF, 0);
if (preferences != null) {
String lock = preferences.getString(IS_SET, null);
if (lock != null) {
if (lock.equals("passcode")) {
gotopasslock(context, intent);
} else {
gotopatternlock(context, intent);
}
}
}
}
public void gotopasslock(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent intent1 = new Intent(context, Main.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
} else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent myStarterIntent = new Intent(context, Main.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
}
}
public void gotopatternlock(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent intent1 = new Intent(context, PatternLock.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
} else if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent myStarterIntent = new Intent(context, PatternLock.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
}
}
}
Main.class
public class Main extends AppCompatActivity {
private PinLockView mPinLockView;
private IndicatorDots mIndicatorDots;
private String TAG = getClass().getName();
String oldPassword = "";
String newPassword = "";
SharedPreferences preferences;
SharedPreferences.Editor editor;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getSupportActionBar().hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.main_xml);
menifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="label">
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="face" />
<activity android:name=".activities.ChooseActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- <category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />-->
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.Home" />
<activity android:name=".activities.MainActivity" />
<activity
android:name=".activities.Main"
android:showOnLockScreen="true" />
<activity
android:name=".activities.PatternLock"
android:showOnLockScreen="true" />
<!--<receiver
android:name=".receiver.ScreenReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
</receiver>-->
<receiver
android:name=".receiver.ScreenReceiver"
android:label="ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
<intent-filter>
<category android:name="android.intent.category.HOME" />
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.SCREEN_OFF" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
Intent Action is missing.
To make a android activity launch after reboot, add following line in
AndroidManifest.xml
STEP 1
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Step 2:
<receiver
android:name=".receiver.ScreenReceiver"
android:label="ScreenReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"
</intent-filter>
</receiver>
NOTE:
try to do code cleanup

Cannot found image when pickup from my app

i have write an app like camera on android. But when pick an imagefrom my app, it have error can not found image.
imvpick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent pickIntent = new Intent("inline-data");
if (pickIntent != null) {
pickIntent.putExtra("data", BitmapFactory.decodeResource(getResources(), R.drawable.picker));
setResult(RESULT_OK, pickIntent);
finish();
}
}
});
and in manifest
<activity android:name=".CameraActivity" android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

Android: ActivityNotFound Exception

Basically the error is in Manifest file I think.
Here is the code for MainActivity.java
private void setInitialScreen( int visibility ) {
Button choose_picture = (Button) findViewById(R.id.choose_picture);
choose_picture.setVisibility(visibility);
choose_picture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.addCategory("choose_file");
Log.d("mainactivity", intent.getCategories().toString());
startActivityForResult(intent, 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(data.hasCategory("choose_file") && resultCode == RESULT_OK) {
Uri uri = data.getData();
setPhotoEditScreen(0, uri);
}
}
private void setPhotoEditScreen( int visibility, Uri uri ) {
View screen_image_editing = findViewById(R.id.screen_image_editing);
screen_image_editing.setVisibility(visibility);
ImageView main_image = (ImageView) findViewById(R.id.main_image);
main_image.setImageURI(uri);
}
and this is the manifest entry
<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>
<intent-filter >
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="choose_file"/>
</intent-filter>
</activity>
Screenshot of Logcat
https://www.dropbox.com/s/w0hi3g7n3i966t6/Screenshot%202014-04-14%2010.33.17.jpg
<intent-filter >
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="choose_file"/>
</intent-filter>
Please do remove the following lines from manifest.xml and try it out , I think since it has two intent filters there might be a problem and in the android name and have you included the package name in the
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.splash_1"
Like this

Unable to implement QR code Reader in my android app

I am trying to implement a QR code reader in my android app. I have followed these steps:
Created a sample library project from the zxing.zip , /android and /core
Added the created library to my app
My code is as below :
public class main extends Activity {
/** Called when the activity is first created. */
Button scanButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scanButton = (Button) findViewById(R.id.button1);
scanButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(
"com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
The scanner doesn't start and I get this error:
05-08 14:12:03.313: ERROR/AndroidRuntime(718): Caused by: java.lang.ClassNotFoundException: com.google.zxing.client.android.CaptureActivity in loader dalvik.system.PathClassLoader[/data/app/com.scanner.demo-2.apk]
The manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.scanner.demo" android:versionCode="1" android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".main" 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="com.google.zxing.client.android.CaptureActivity"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Why do you put this in your Manifest?
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
...
</activity>
Are you calling internal activity or the App from Zxing?
This is quite confused. You seem to be wanting to integrate by Intent, which is what the first half does. But then you seem to have copied our Manifest. Why? Please remove that. It's not needed, causes your error, and if you leave it in, interferes with our app.
This is all you need: https://code.google.com/p/zxing/wiki/ScanningViaIntent

onActivityResult not being called

KitActivity:
/** in KitActivity, handler invoked after successful transmission **/
private final Handler txHandle = new Handler() {
#Override
public void handleMessage(Message msg) {
boolean success = msg.getData().getBoolean("success");
dismissDialog(DIALOG_TX_PROGRESS);
if(success) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putInt("previous_scale", mScaleSpn.getSelectedItemPosition());
editor.commit();
//clearFields();
//showDialog(DIALOG_ETX);
KitActivity.this.setResult(0);
KitActivity.this.finish();
} else {
removeDialog(DIALOG_FAIL);
showDialog(DIALOG_FAIL);
}
}
};
MainActivity:
/** in the MainActivity **/
public void startCreateKit() {
Intent i = new Intent(MainActivity.this, KitActivity.class);
startActivityForResult(i,0);
}
protected void onActivityResult(int reqCode, int resCode) {
if(reqCode==0) {
if(resCode==0) {
//we good, perform sync
showDialog(DIALOG_TX_PROGRESS);
Toast.makeText(this, "Performing Auto Sync", Toast.LENGTH_LONG).show();
updateKits();
} else {
//uh oh
}
}
}
createKitBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast.makeText(context, "NEW KIT", Toast.LENGTH_SHORT).show();
startCreateKit();
}
});
onActivityResult is never called in MainActivity. this is pretty much by the book. what's the issue?
stuff i've tried:
- using Activity.RESULT_OK for the result code (which translates to -1);
- removing the setResult() and finish() calls from the handler and calling an outside method to invoke them.
i don't see anything wrong. here's the manifest, nothing awry here:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.conceptualsystems.kitmobile"
android:versionCode="8"
android:versionName="#string/version">
<application android:label="#string/app_name" android:icon="#drawable/icon" android:debuggable="true">
<activity android:name="MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ShipActivity"
android:label="Ship Kits"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
<activity android:name="KitActivity"
android:label="Kit Entry"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
<activity android:name="ColorActivity"
android:label="Color Selection"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
what gives?
in the activity you start (KitActivity.class) on success you do this
Intent intent = this.getIntent();
intent.putExtra("SOMETHING", "EXTRAS");
this.setResult(RESULT_OK, intent);
finish();
else you put RESULT_CANCELED instead of RESULT_OK
replaced:
protected void onActivityResult(int reqCode, int resCode)
with:
protected void onActivityResult(int reqCode, int resCode, Intent intent)
amazing what happens when you review the documentation... -_-

Categories

Resources