How to Set new Admin - android

I am using the Device Policy Manager to lock the android phone immediately and here is my activity code:
package com.husam.admin;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
public class AdminActivity extends Activity {
/** Called when the activity is first created. */
protected static final int REQUEST_CODE_ENABLE_ADMIN=1;
DevicePolicyManager dpm;
ComponentName mAdminName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dpm=(DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mAdminName=new ComponentName(this,MyAdmin.class);
Button button=(Button)findViewById(R.id.admin);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(dpm.isAdminActive(mAdminName))
{
Log.w("Yes admin","Locking Now");
dpm.lockNow();
}
else
{Intent intent1 =new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent1.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent1.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Need new Admin");
Log.w("no Admin","Set admin");
startActivityForResult(intent1,REQUEST_CODE_ENABLE_ADMIN);
}
}
});
}
class MyAdmin extends DeviceAdminReceiver{
void OnEnabled(){
}
void onDisable(){
}
}
}
and My Mainfest.xml fill is as follow:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.husam.admin"
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=".AdminActivity"
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=".MyAdmin"
android:label="#string/device_admin"
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>
</application>
</manifest>
and my_admin.xml which is inside xml folder under res folder is as follows:
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>
and when I run my app I faced an error in my Log file state that:
W/DeviceAdminAdd(433): Unable to retrieve device policy ComponentInfo{com.husam.admin/com.husam.admin.AdminActivity$MyAdmin}
I searched this website for similar issued but all what I found is that the error was in the mainfest file in closing the receiver before including the meat and intent filter inside it which is not as my case.
So any help or redirection to solve this issue will be completely appreciated
regards
Husam

I think I figured out the answer to the question I posted above. What I need to do is that in registering my receiver in my mainfest file I have to do the following:
<receiver
android:name=".AdminActivity$MyAdmin"
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>
Note:
in android:name I have to write .AdminActivity$Myadmin instead of .Myadmin, since my class for administration broadcast receiver is an inner class in my activity, not a separate one.

Related

Launch Activity in android via Dialpad using Secret Code

I just want to launch activity via Secret Code .
Although , there are many solutions available for this problem but none of them worked for me. It seems like BroadcastReceiver is not listening .
I am using Mi4i MIUI 7.
Here's my code. Please Help!!
MainActivity.java
package com.example.jatin.myapplication2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have even tried the below commented code , but that didn't work.
MySecretCodeReceiver.java
package com.example.jatin.myapplication2;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Jatin on 05-Aug-16.
*/
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
String uri = intent.getDataString();
String sep[] = uri.split("://");
if (sep[1].equalsIgnoreCase("1234"))
{
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.example.jatin.myapplication2.MainActivity");
context.startActivity(launchIntent);
}
else if (sep[1].equalsIgnoreCase("5678")) {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("net.one97.paytm");
context.startActivity(launchIntent);
}
// Intent i = new Intent(context, MainActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// setResultData(null);
// context.startActivity(i);
}
}
}
I have also tried putting android:host="1234" in manifest ,still not getting desired results
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
<receiver android:name="receivers.MySecretCodeReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.PROCESS_OUTGOING_CALLS">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" />
</intent-filter>
</receiver>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Got it ....
We have to register our MySecretCodeReceiver as a <receiver> within the <application> element in the AndroidManifest.xml file.
And , Its Done. :)

Calling another class from MainActivity in OnCreate()

I have two main classes in my application for activity launch as.
MainActivity.Java
package com.connect;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends Activity {
private PolicyManager policyManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
policyManager = new PolicyManager(this);
System.out.println("Hello");
if (!policyManager.isAdminActive()) {
Intent activateDeviceAdmin = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
activateDeviceAdmin.putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN,
policyManager.getAdminComponent());
activateDeviceAdmin
.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"After activating admin, you will be able to block application uninstallation.");
startActivityForResult(activateDeviceAdmin,
PolicyManager.DPM_ACTIVATION_REQUEST_CODE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == Activity.RESULT_OK
&& requestCode == PolicyManager.DPM_ACTIVATION_REQUEST_CODE) {
// handle code for successfull enable of admin
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Droidian.java
package com.connect;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import java.io.IOException;
public class Droidian extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
PackageManager i = getApplicationContext().getPackageManager();
i.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
if (isMyServiceRunning() == false) {
startService(new Intent(getApplicationContext(), DroidianService.class));
Log.i("com.connect", "startService");
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (DroidianService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
After executing MainActivity.java , Droidian.java should run as this is the class which would be doing all the function. I don't know how to call the other class in MainActivity.java class. Here is my Manifest file snippet.
Android Manifest.xml
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.connect"
android:versionCode="1"
android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" tools:ignore="OldTargetApi"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<supports-screens android:resizeable="true"
android:largeScreens="true"
android:xlargeScreens="true"
/>
<application
android:allowBackup="false"
android:icon="#drawable/launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:persistent="true">
<activity
android:name="com.connect.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:name="com.connect.SampleDeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/device_admin" />
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
<activity
android:name="com.connect.Droidian"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You cant declare two activities as LAUNCHER. change your droidian activity as DEFAULT.
<activity
android:name="com.connect.Droidian"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Add this to your onCreate
Intent intent = new Intent(getApplicationContext(), Droidian.class);
startActivity(intent);
A few issues:
At first, there is the launcher thing Yogesh mentioned. The launcher category in the intent filter means, that this Activity is the one that gets called first, thus there can only exist one launcher.
It isn't exactly clear if you really want Droidian to be a Activity, since you plainly call it a class. Is it meant to have its own view, or should it be a worker class? If the later, don't extend Activity, just make it a regular class. You then can call the functions by instanciating it in your MainActivity like Droidian mDroidian = new Droidian() or making your functions static.
If you really intent to use a Activity and 'call onCreate', you have to start the Activity. Certain methods can't be started manually but are called automatically once you start an Activity, see the Android reference on Activities, especially the 'Activity Lifecycle' section.
The easiest way to start an Activity would be
Intent intent = new Intent(MainActivity.this, Droidian.class);
startActivity(intent);
Also you might want to have a look at the Android Developer trainings, especially the one about starting an activity.

Why app not launching on booting android device?

I am testing the following app on HTC with Android 2.3.5. For some reason app is not launching on restarting or booting the phone.
I need to know where am I wrong exactly?
BootReciever.java
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;
public class BootupReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "App started", Toast.LENGTH_LONG).show();
/*
Intent startActivityIntent = new Intent(context, MainActivity.class);
startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startActivityIntent);
*/
// context.startActivity(new Intent(context, MainActivity.class));
}
}
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.content.BroadcastReceiver;
import android.widget.Toast;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Android Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidbootreciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.hello_android_world.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:name="com.example.androidbootreciever.BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
The code is working on restart but not on turning on the phone after shutting it down. Here are the messages that I receive in my logcat:
You extended BootupReceiver with Activity?
It should be extended with Broadcast Receiver.
public class BootupReceiver extends BroadcastReceiver{
}
Your Receiver:
public class AutoStartBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent receivedIntent) {
context.startActivity(new Intent(context, YourActivity.class));
}
}
Your Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.receiver.AutoStartBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Good luck!
You need to write following permission,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
If your application is stored on External storage then you should provide following intent filter,
<receiver android:name=".BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
See App Install Location.

start my android app when usb connect

I try to write app which can start when usb connect
I learn from Starting my android application automatically after connecting the USB cable.
my code is
package com.example.formatsdcard;
import java.io.File;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MainActivity extends Activity {
public class OnPowerReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mButton01 = (Button)findViewById(R.id.button1);
mButton01.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
wipeMemoryCard();
}
});
MY Manifest is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.formatsdcard"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.formatsdcard.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:name=".OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
</application>
my app can run after I click it but it will show there're some problem try again later when I connect my usb cable.
How should I change my code to let it work normally.
You should probably try
<receiver android:name="MainActivity$OnPowerReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
Or try putting OnPowerReceiver as a public class in your package com.example.formatsdcard
Create a own class for your onPowerReceiver. Don't put it in your MainActivity.

Why does my app take twice to open?

My app for some reason fails the first time it opens, but then opens fine the second time I click on it. Are there any common reasons this happens?
Here is the code for the main activity, any information would be greatly appreciated:
package com.chich;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class chich_activity extends Activity
{
Button btnSendSMS;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ComponentName locationReceiver = new ComponentName(this, SmsReceiver.class);
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(locationReceiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(chich_activity.this, chich_activity2.class);
startActivity(i);
}
});
}
}
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chich"
android:versionCode="1"
android:versionName="1.0.0">
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<activity android:name=".chich_activity"
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=".chich_activity2"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="9999999" >
<action android:name = "android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS">
</uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
</manifest>
The answer came in the package manager. Turns out it shuts the application off if you don't write in the end, PackageManager.DONT_KILL_APP as in:
ComponentName locationReceiver = new ComponentName(this, SmsReceiver.class);
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(locationReceiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Try to leave only one activity as launcher. Remove this row <category android:name="android.intent.category.LAUNCHER" /> from here
<activity android:name=".chich_activity2"
android:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I had same issue, is not the best way but work for me!
switch(estado) {
case 0:
if (!tcursor.moveToFirst()){
estado = 1;
Intent intentConfig=new Intent(getApplicationContext(),Configuracion.class);
startActivity(intentConfig);
}
break;
default:
}

Categories

Resources