Run Android app as Administrator - android

I'm trying to write a simple app, and follow the ApiDemos sample. My app is crashing on startup. I suspect a problem with my manifest, but I'm not sure. Any ideas please?
public class managerActivity extends DeviceAdminReceiver {
public class Controller extends Activity {
static final int RESULT_ENABLE = 1;
DevicePolicyManager mDPM;
ActivityManager mAM;
ComponentName mDeviceAdminSample;
Button mEnableButton;
Button mDisableButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mAM = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
mDeviceAdminSample = new ComponentName(Controller.this, managerActivity.class);
setContentView(R.layout.main);
}
}
and then in my manifest, I have:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="com.appname.managerActivity$Controller"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</activity>
<!-- Device Admin Samples -->
<receiver android:name="com.appname.managerActivity"
android:label="#string/sample_device_admin"
android:description="#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" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
Here's the crash:
11-11 15:20:52.310: ERROR/AndroidRuntime(553):
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.appname.manager/com.appname.managerActivity$Controller}:
java.lang.InstantiationException:
com.appname.managerActivity$Controller

The nested class cannot be created from outside of its enclosing class, unless it's static. Move your activity to a standalone class (i. e. not nested), or make the activity class static.

Related

How do I specify multiple activities in AndroidManifest one for splash screen, one for mainactivity - pass the original intent to the mainactivy

I am only new to android this is my first application.
I am making progress on an app that fires when the NFC reader detects a TAG that contains NDEF messages, specifically when a URL is detect matching my domain.
I would like to add a splashscreen that fires triggered by the Android INTENT of but then passes the original INTENT to the mainactivity for further processing, I have made a start but not sure how to marry up the manifest and code to do what I am after.
MANIFEST.XML
<activity
android:name=".SplashScreenActivity"
android:theme="#style/Theme.MyApp.SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" android:theme="#style/Theme.MyApp.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="<my custom domain>"/>
</intent-filter>
</activity>
Any help on how I should structure my Manifest to deal with this scenario, the SplashScreenActivity I could use the intent filters here to ensure that it is triggered, but then I create a new intent which means losing the NDEF extras.
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
finish();
}
}
I would like to be able to "just pass" the whole original INTENT to the mainactivity but I am unsure how to do this.
Here is the code that extracts the NDEF messages from the INTENT any ideas appreciated.
private void readFromIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
logViewModel.insert(new LogEntry("READFROMINTENT ACTUALLY FIRED", "Action " + intent.getExtras()));
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
getNdefRecords(msgs);
addLinkToDbFromRecord(records);
}
//buildTagViews(msgs);
}
}
So I managed to solve this by doing the following.
I moved my intent filters from MainActivity to the .splashscreen actvity changed the category of mainactivity to DEFAULT, and allowed splashscreen activity to remain the LAUNCHER
<activity
android:name=".MainActivity"
android:theme="#style/Theme..NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.myap.app.SplashScreenActivity"
android:theme="#style/Theme.myapp.SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="my domain"/>
</intent-filter>
</activity>
In my splashscreen activity i used the following code to copy the extras from the original intent, set the action to the expected action the mainactivity was designed to handle and used this to start the main activity
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent startMainActivity = new Intent(SplashScreenActivity.this, MainActivity.class);
startMainActivity.putExtras(getIntent());
startMainActivity.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
startActivity(startMainActivity);
finish();
}
}

Flutter LockTask

I want to lockdown a flutter applicatio with the android locktask. I tried several approaches but the setLockTaskPackages method doesn't seem to do anything. At first I tried to call the method in the onCreate function of the MainActivity. Then I tried to call it over a method channel but both approaches didn't work. Here is my onCreate funtion with method channel:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
mDevicePolicyManager = (DevicePolicyManager) this.getSystemService(Context.DEVICE_POLICY_SERVICE);
mAdminName = new ComponentName(this, Receiver_Admin.class);
new MethodChannel(getFlutterView(), "com.packagename/kiosk").setMethodCallHandler(
new MethodCallHandler() {
#Override
public void onMethodCall(MethodCall call, Result result) {
mDevicePolicyManager.setLockTaskPackages(
mAdminName,
new String[]{"com.packagename"}
);
result.success(null);
}
}
);
}
My AndroidMainifest.xml:
<receiver
android:name=".receiver.Receiver_Admin"
android:label="Sehne"
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.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="#style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in #style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Any ideas why nothing happens?
Thanks in advance

Direct share is not working with my app. ChooserTargetService is not called

I referred sample application code (https://github.com/googlesamples/android-DirectShare) and I added ChooserTargetService to my application
public class ShareChooserTargetService extends ChooserTargetService {
#Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
IntentFilter matchedFilter) {
Log.d("ShareChooserTargetService", "onGetChooserTargets: ");
ComponentName componentName = new ComponentName(getPackageName(),
ShareActivity.class.getCanonicalName());
ArrayList<ChooserTarget> targets = new ArrayList<>();
for (User user : Users.getAll()) {
Bundle extras = new Bundle();
extras.putInt("user_id", user.id);
targets.add(new ChooserTarget(
user.name,
Icon.createWithResource(this, R.mipmap.ic_user),
componentName,
extras));
}
return targets;
}
In Manifest file I added these lines:
<activity
android:name=".ui.ShareActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<meta-data
android:name="android.service.chooser.chooser_target_service"
android:value=".ShareChooserTargetService" />
</activity>
<service
android:name=".ShareChooserTargetService"
android:label="#string/app_name"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
<intent-filter>
<action android:name="android.service.chooser.ChooserTargetService" />
</intent-filter>
</service>
But ShareChooserTargetService is not called, log isn't printed. I run sample application in my phone and it works perfectly but these code in my app isn't working. I am unable to find what is the issue. Can anyone help please?

Activity B starts before Activity A

I was trying to get a small image to show up before my Main Activity starts. This is my current coding in the android manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
whatever I do, my splash screen does not start. They start separately, but never together, (I still haven't put a timer in my splash image as I want to check whether it works or not and it isn't working)
Remove
<intent-filter>
<action android:name="com.example.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Use below code in splash screen after desired time
Intent intent=new Intent(this,MAINACTIVITY.class);
startActivity(intent);
finish(); //To close splashscreen when MAINACTIVITY loads
The above code starts with splash screen and after some time start your main activity
You should remove the <intent-filter> section from your MainActivity declaration ,
and launch the MainActivity from the splashActivity using a simple intent and startActivity call.
Try this.
<activity
android:name=".Splash"
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=".MainActivity" >
</activity>
</application>
Code for your Splash Class-
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
public class Splash extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{ // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
};
handle.postDelayed(delay,5000);
}
}
its delay next intent 5 second. you can set time according to you.
Use This:
<activity
android:name=".Splash"
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=".MainActivity" >
</activity>
</application>
and This :
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent mInHome = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mInHome);
Splash.this.finish();
}
}, 3000);
}
}

android integrating library app

I am trying to integrate an open source app into my android app. I have made the open source app as a library app and integrated the xml into my android manifest file as well. There are no compile errors.
First Screen is the login screen for the library app and when it is called it is throwing java lang class exception error at:
m_app = (TodoApplication) getApplication();
source code of loginscreen.java:
public class LoginScreen extends Activity {
final static String TAG = LoginScreen.class.getSimpleName();
private TodoApplication m_app;
private Button m_LoginButton;
private BroadcastReceiver m_broadcastReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
m_app = (TodoApplication) getApplication();
// supposed to help with the banding on the green background
findViewById(R.id.loginbackground).getBackground().setDither(true);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.todotxt.todotxttouch.ACTION_LOGIN");
m_broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, TodoTxtTouch.class);
startActivity(i);
finish();
}
};
registerReceiver(m_broadcastReceiver, intentFilter);
m_LoginButton = (Button) findViewById(R.id.login);
m_LoginButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
login();
}
});
//final RemoteClient remoteClient = m_app.getRemoteClientManager()
// .getRemoteClient();
//if (remoteClient.isAuthenticated()) {
switchToTodolist();
//}
}
private void switchToTodolist() {
Intent intent = new Intent(this, TodoTxtTouch.class);
startActivity(intent);
finish();
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(m_broadcastReceiver);
}
void login() {
final RemoteClient client = m_app.getRemoteClientManager()
.getRemoteClient();
if (!client.isAvailable()) {
Log.d(TAG, "Remote service " + client.getClass().getSimpleName()
+ " is not available; aborting login");
Util.showToastLong(m_app, R.string.toast_login_notconnected);
} else {
RemoteLoginTask loginTask = client.getLoginTask();
loginTask.showLoginDialog(this);
}
}
}
Integrated library code in android manifest.xml:
<activity android:name="com.todotxt.todotxttouch.LoginScreen" android:label="#string/app_label"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="com.todotxt.todotxttouch.category.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.todotxt.todotxttouch.TodoApplication" />
<activity android:name="com.todotxt.todotxttouch.Filter" android:label="Filter"
android:theme="#android:style/Theme.NoTitleBar" />
<activity android:name="com.todotxt.todotxttouch.Preferences" android:label="#string/set_preferences" />
<activity android:name="com.todotxt.todotxttouch.AddTask" android:label="#string/addtask"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity-alias android:name="com.todotxt.todotxttouch.AddTaskShortcut"
android:targetActivity="com.todotxt.todotxttouch.AddTask" android:label="#string/shortcut_addtask_name">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>
<activity-alias android:name="com.todotxt.todotxttouch.AddTaskShare"
android:targetActivity="com.todotxt.todotxttouch.AddTask" android:label="#string/share_addtask_name">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity-alias>
<activity android:name="com.todotxt.todotxttouch.HelpActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<activity android:name="com.todotxt.todotxttouch.TodoTxtTouch" android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Could anyone please help me on understanding the problem.
Let me explain further: I am having a file called Todoapplication.java....so the class exists...it being called from LoginScreen.java as
m_app = (TodoApplication) getApplication();
and that is where I am getting java lang class exception?
Activity.getApplication() returns an instance of the application class that was declared in the manifest in the <application> element. I don't see it in your pasted manifest.
It's not enough to simply have the application class in your app. It must be explicitly designated as one in the manifest.
I maybe getting the wrong end of the stick so I beg the programming gods for forgiveness in advance.
Assuming you are developing in Eclipse, is this not a simple case of having a project in Eclipse with the open source source in, which within the project properties has the option isLibrary ticked.
In YOUR project's properties you can add a library and Eclipse will list the open source one (and any others that have the "isLibrary" checked). Would you not simply select the open source project and add it. Your project will then add the library and build again?
To access the open source project, now a library, you can use "import" statements to access any public methods that are exposed.
A good example of this setup process using an open source library project is Actionbar Sherlock to which I wrote a tutorial youtube that demonstrates visually what I have just written. It can be found at http://www.youtube.com/watch?v=avcp6eD_X2k

Categories

Resources