I am trying this command from adb shell
dpm set-device-owner com.demoapp.amm/DeviceAdminSample
and i am getting this response.
Unknown admin: ComponentInfo{com.demoapp.amm/DeviceAdminSample}
com.demoapp.amm is my package name in Manifest file.
Below is my receiver from Manifest file
<receiver
android:name=".DeviceAdminSample"
android:description="#string/sample_device_admin_description"
android:label="#string/sample_device_admin_description"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/device_admin_sample" >
</meta-data>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
Below is My Class DeviceAdminSample.class
package com.demoapp.amm;
import android.app.admin.DeviceAdminReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class DeviceAdminSample extends DeviceAdminReceiver {
void showToast(Context context, String msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
#Override
public void onEnabled(Context context, Intent intent) {
showToast(context,"Enabled");
}
#Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return "Disabled";
}
#Override
public void onDisabled(Context context, Intent intent) {
showToast(context, "Disabled");
}
#Override
public void onPasswordChanged(Context context, Intent intent) {
showToast(context,"Password Change");
}
public static ComponentName getComponentName(Context context) {
return new ComponentName(context.getApplicationContext(),
DeviceAdminSample.class);
}
}
It's because you've prefixed a period to the receiver name within your manifest android:name=".DeviceAdminSample" Thus com.demoapp.amm/DeviceAdminSample doesnt exist.
Try instead dpm set-device-owner com.demoapp.amm/.DeviceAdminSample.
Related
I am unabe to load a message(log) when boot is completed. I have tried with below code:
In BootReceiver.java file:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class BootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Booting Completed", Toast.LENGTH_LONG).show();
Log.d("On Boot Load", "Boot loading");
}
}
In Services.java file:
public class UpdateService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
Log.d("Service", "Updating Services");
return null;
}
}
I have also added with below code in menifes.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".receivers.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
When I run the app the log does not appear in logcat section.
The receiver tag should be inside application tag and the uses-permission tag should be outside application tag but inside of manifest tag.
You should use onStartCommand() implementation:
public class UpdateService extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
Log.d("Service", "Updating Services");
return null;
}
#Override
public int onStartCommand(Intent pIntent, int flags, int startId) {
Log.d("On Boot Load", "Boot loading");
return super.onStartCommand(pIntent, flags, startId);
}
}
Also beware of other filter like android.intent.action.QUICKBOOT_POWERON.
I use this code (without a service) and it works for me (just tested it):
AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name=".MyBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Kotlin:
class MyBootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == ACTION) {
Logger.warning("BOOTING UP!")
}
}
companion object {
const val ACTION = "android.intent.action.BOOT_COMPLETED"
}
}
I want to activate startLockTask() without any request for users. So at first i read this Device Administration
and create small sample application. In result i have my application in device administrators list but when i call startLockTask() it still execute not in silent mode.
When i'm trying to call it so:
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
String[] packages = {this.getPackageName()};
myDevicePolicyManager.setLockTaskPackages(deviceAdminComponentName, packages);
startLockTask();
i'm getting next exception :java.lang.SecurityException: Admin ComponentInfo{com.hssoft.deviceadmintest/com.hssoft.deviceadmintest.DeviceAdmin} does not own the device
So i have Device administrator but i'm not device owner. What i'm doing wrong, can somebody help?
Here is my full app code:
Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hssoft.deviceadmintest">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.hssoft.deviceadmintest.DeviceAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="#xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.PROFILE_PROVISIONING_COMPLETE"/>
</intent-filter>
</receiver>
</application>
</manifest>
xml/device_admin.xml:
<?xml version="1.0" encoding="utf-8"?>
<device-admin>
<uses-policies>
<limit-password/>
<watch-login/>
<reset-password/>
<force-lock/>
<wipe-data/>
<expire-password/>
<encrypted-storage/>
<disable-camera/>
</uses-policies>
</device-admin>
DeviceAdmin.java:
package com.hssoft.deviceadmintest;
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class DeviceAdmin extends DeviceAdminReceiver {
#Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
Log.i("Device Admin: ", "Enabled");
}
#Override
public String onDisableRequested(Context context, Intent intent) {
return "Admin disable requested";
}
#Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
Log.i("Device Admin: ", "Disabled");
}
#Override
public void onPasswordChanged(Context context, Intent intent) {
super.onPasswordChanged(context, intent);
Log.i("Device Admin: ", "Password changed");
}
}
MainActivity.java:
package com.hssoft.deviceadmintest;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
static final int ACTIVATION_REQUEST = 1;
ComponentName deviceAdminComponentName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
deviceAdminComponentName = new ComponentName(this, DeviceAdmin.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponentName);
startActivityForResult(intent, ACTIVATION_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTIVATION_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Log.i("MainActivity", "Administration enabled!");
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
String mPackageName = this.getPackageName();
if (myDevicePolicyManager.isDeviceOwnerApp(mPackageName)) {
myDevicePolicyManager.setLockTaskPackages(deviceAdminComponentName, new String[]{mPackageName});
}
if (myDevicePolicyManager.isLockTaskPermitted(mPackageName)) {
startLockTask();
}
} else {
Log.i("MainActivity", "Administration enable FAILED!");
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
your application has to be a device owner to behave as you want, if you have root access you can run this command to achieve that
adb shell dpm set-device-owner com.hssoft.deviceadmintest/.DeviceAdmin
this article will help you alot.
My Requirment
I am trying to build an application which shows a Toast whenever user makes a outgoing call from the phone. For this I am using a BroadcastReceiver to tap the call action and a service (to run Receiver always). once I start this activity, it starts showing toast when a outgoing call get initiated ...everything works well.
Issues
Sometimes I dont get any Toast Action EVEN for Outgoing call as well, is that mean service stops after some time ?
After phone reboot Toast Action for Outgoing call stops. Untill you start the servce again manually.
The code I have written is ok ? OR can it be improved ?
Below is the complete code -
MainActivity.class
public class MainActivity extends Activity
{
CallNotifierService m_service;
boolean isBound = false;
private ServiceConnection m_serviceConnection = new ServiceConnection()
{
#Override
public void onServiceConnected(ComponentName className, IBinder service)
{
m_service = ((CallNotifierService.MyBinder)service).getService();
Toast.makeText(MainActivity.this, "Service Connected", Toast.LENGTH_LONG).show();
isBound = true;
Intent intent = new Intent(MainActivity.this, CallNotifierService.class);
startService(intent);
}
#Override
public void onServiceDisconnected(ComponentName className)
{
m_service = null;
isBound = false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, CallNotifierService.class);
bindService(intent, m_serviceConnection, Context.BIND_AUTO_CREATE);
}
.
.
.
}
CallNotifierService.class
public class CallNotifierService extends Service
{
private final IBinder myBinder = new MyBinder();
private static final String ACTION_OUTGOING_CALL = "android.intent.action.NEW_OUTGOING_CALL";
private static final String ACTION_ANSWER = "android.intent.action.ANSWER";
private static final String ACTION_CALL = "android.intent.action.CALL";
private CallBr br_call;
#Override
public IBinder onBind(Intent arg0)
{
return myBinder;
}
#Override
public void onDestroy()
{
Log.d("service", "destroy");
this.unregisterReceiver(this.br_call);
Toast.makeText(CallNotifierService.this, "Receiver Un-Registered", Toast.LENGTH_LONG).show();
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
final IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_ANSWER);
filter.addAction(ACTION_CALL);
filter.addAction(ACTION_OUTGOING_CALL);
this.br_call = new CallBr();
this.registerReceiver(this.br_call, filter);
Toast.makeText(CallNotifierService.this, "onStartCommand Called", Toast.LENGTH_LONG).show();
return START_STICKY;
}
public class MyBinder extends Binder
{
CallNotifierService getService()
{
return CallNotifierService.this;
}
}
public class CallBr extends BroadcastReceiver
{
public CallBr() {}
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Action:"+intent.getAction(), Toast.LENGTH_LONG).show();
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alwaysrunningprocesswithcallanswertap"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-sdk
android:minSdkVersion="22"
android:targetSdkVersion="22" />
<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>
<service android:name="com.example.alwaysrunningprocesswithcallanswertap.CallNotifierService" />
</application>
</manifest>
Could anyone please help for the issue OR pointout something better ?
#Bharat Once device switch off automatically service will stop, So when ever your device reboot that time you need to start service again then only you will get Toast Action, refer this example code :
BootCompleteReceiver.java
package com.example.newbootservice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, AutostartService.class);
context.startService(service);
}
}
AutostartService .java
package com.example.newbootservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class AutostartService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
MainActivity.java
package com.example.newbootservice;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(getBaseContext(), AutostartService.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newbootservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service android:name=".AutostartService"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<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>
</application>
</manifest>
I have a BroadcastReceiver named StartService which start when the phone boots which in turn starts a service but when I am going to the service from this BroadcastReceiver I get exception:
android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to bind to services
my code is below:
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
public class StartService extends BroadcastReceiver {
private RemoteServiceConnection conn = null;
private IMyRemoteService remoteService;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent startServiceIntent = new Intent(context, RemoteServiceClient.class);
context.startService(startServiceIntent);
conn = new RemoteServiceConnection();
Intent i = new Intent();
i.setClassName("com.collabera.labs.sai", "com.collabera.labs.sai.RemoteService");
context.bindService(i, conn, Context.BIND_AUTO_CREATE);
}
class RemoteServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName className,
IBinder boundService ) {
remoteService = IMyRemoteService.Stub.asInterface((IBinder)boundService);
Log.d( getClass().getSimpleName(), "onServiceConnected()" );
}
public void onServiceDisconnected(ComponentName className) {
remoteService = null;
Log.d( getClass().getSimpleName(), "onServiceDisconnected" );
}
};
}
some of the step follow.
Step 1:
public class StartService extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, myServices.class);
context.startService(startServiceIntent);
}
}
Step 2:
// make all service related task
Step 3:
<service
android:name=".myServices"
android:enabled="true" />
<receiver android:name="com.yourpackage.StartService " >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I am always used BroadcastReceiver and services this way.
May be helpful for you.If helpful then accept answer.
I would like to bring up a Toast and vibrate when call status is Idle.
I added this in the manifest file:
<receiver android:name="IncomingCallInterceptor">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
And this is IncomingCallInterceptor
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import android.app.Activity;
public class IncomingCallInterceptor extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String msg = "Phone state changed to " + state;
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
{
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
msg += ". Incoming number is " + incomingNumber;
}
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
if(msg == "IDLE")
{
ok() ;
}
}
public void ok()
{
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(3000);
}
}
}
And I received this error:
Description Resource Path Location Type
The method getSystemService(String) is undefined for the type IncomingCallInterceptor IncomingCallInterceptor.java line 39 Java Problem
Make Sure You Have Added VIBRATE permission in manifest as:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
<uses-permission android:name="android.permission.VIBRATE"/>
<application android:label="...">
...
</application>
</manifest>
change Your Code as:
public class IncomingCallInterceptor extends BroadcastReceiver {
private Context contextc;
#Override
public void onReceive(Context context, Intent intent)
{
this.contextc=context;
//YOUR CODE HERE
public void ok()
{
Vibrator v = (Vibrator)contextc.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(3000);
}