I was following the example code for the Device Admin app. My attempts to start activity that enables the user to add a device admin app has failed. The following is the code which I use to try and start this.
public class DeviceAdminTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ComponentName deviceAdminReceiver = new ComponentName(this, TestAdminReceiver.class);
Intent i = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
i.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminReceiver);
i.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "My Explanantion");
// startActivityForResult(i, 1);
try{
startActivity(i);
}catch(Exception ex){
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG);
}
}
}
The following is the code of the Receiver...
public class TestAdminReceiver extends DeviceAdminReceiver {
#Override
public void onEnabled(Context context, Intent intent) {
Toast.makeText(context, "On Enabled", Toast.LENGTH_SHORT).show();
}
#Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return ("You are about to disable device admin");
}
#Override
public void onDisabled(Context context, Intent intent) {
Toast.makeText(context, "On Disabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onPasswordChanged(Context context, Intent intent) {
Toast.makeText(context, "On Password Changed", Toast.LENGTH_SHORT).show();
}
}
And finally, here is the manifest...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.zeezulander.admintest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="org.zeezulander.admintest.DeviceAdminTestActivity"
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=".TestAdminReceiver" android:permission="android.permission.BIND_DEVICE_ADMIN" android:label="Test Admin">
<meta-data android:resource="#xml/admin_data" android:name="adminMetaData"/>
<intent-filter >
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>
I do not get an error even. My Activity starts, but nothing happens beyond, no error even.
Maybe something is wrong with the layout. If you're not getting an error, then it isn't crashing, and if it isn't crashing then the only thing that could be wrong is the display (layout).
final Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.DeviceAdminAdd"));
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminReceiver);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "To use all features activate it.");
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
startActivityForResult(intent, ACTIVATION_REQUEST);
Related
This is little snipet to try BroadcastReciever android but i dont understand wath's wrong.
In fact just callIntent return log. my little brain it's burning.
if somebody can tell me why ?
Sorry for my bad english !
```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.plectre.broadcast">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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=".MyReciever" >
<intent-filter>
<action android:name="com.plectre"/>
</intent-filter>
</receiver>
</application>
</manifest>
```
public void callIntent() {
Intent intent = new Intent();
intent.setAction("com.plectre");
intent.putExtra("frist", "Intent premier" );
sendBroadcast(intent);
Log.i("callIntent", "Tag");
}
```
public class MyReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//String result = intent.getStringExtra("first");
Toast.makeText(context, "Result", Toast.LENGTH_LONG).show();
Log.i("Broadcast", " Receiver");
}
```
No error, no crash, nothing !! :=(
Create an explicit intent by explicitly declaring the receiver when you create the intent. At present your intent is implicit.
public void callIntent() {
// Explicitly declare the receiver class
Intent intent = new Intent(this, MyReciever.class);
intent.setAction("com.plectre");
intent.putExtra("frist", "Intent premier" );
sendBroadcast(intent);
Log.i("callIntent", "Tag");
}
Additional reading: https://developer.android.com/guide/components/intents-filters#ExampleExplicit
Also. You declare the intent's Extra as "frist" and try to receive at as "first". This needs to be rectified.
You should ensure that the correct action is taken with the specified Intent Action in your receiver:
#Override
public void onReceive(Context context, Intent intent) {
if ("com.plectre".equals(intent.getAction())) {
final String result = intent.getStringExtra("first");
Toast.makeText(context, "Result", Toast.LENGTH_LONG).show();
Log.i("Broadcast", " Receiver");
}
}
I've began working on a simple Android project recently, but it looks like I just cannot get it to work. I have a main Activity (It has #style/Invisible parameter) which checks if my service is running and starts it if it's not. Also, before the check takes place it starts another activity for result. The activity is expected to return the user's username and password to allow the app to login to the system. The problem is that when I install the app on my phone it works fine, but next time I open the app nothing happens. I have to reinstall it.
Here's the main Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Intent intent = new Intent(this, LoginGrabber.class);
startActivityForResult(intent, 100);
if(isMyServiceRunning()==false)
{
startService(new Intent(getApplicationContext(), MyService.class));
Log.i("com.connect","startService");
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 100){
if(resultCode == RESULT_OK){
String username = data.getStringExtra("Username");
String password = data.getStringExtra("Password");
//TODO Send to server
Toast.makeText(this, "Username: " + username + " Password: " + password, Toast.LENGTH_LONG);
}
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
LoginGrabber Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_grabber);
}
public void onSendData(View view){
Intent intent = new Intent();
intent.putExtra("Username", ((TextView) findViewById(R.id.email)).getText());
intent.putExtra("Password", ((TextView) findViewById(R.id.password)).getText());
setResult(RESULT_OK, intent);
finish();
}
Here's the Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.test"
android:versionCode="2"
android:versionName="2.0">
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" />
<supports-screens
android:largeScreens="true"
android:resizeable="true"
android:xlargeScreens="true" />
<uses-permission
android:name="android.permission.INTERNET"
android:required="true" />
<application
android:icon="#drawable/launcher"
android:label="#string/app_name"
android:theme="#style/Invisible">
<activity
android:name="com.connect.Main"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
<service
android:name="com.connect.MyService"
android:enabled="true"
android:exported="true" />
<activity
android:theme="#style/AppTheme"
android:name="com.connect.LoginGrabber"
android:label="#string/title_activity_login_grabber" />
</application>
</manifest>
What am I doing wrong?
Thanks for your help!
OK! I found the problem! The problem was that my application was crashing as soon as the service was started because I was testing the app without internet and without handling errors that were thrown when there was no internet available.
Just had to turn WiFi on, that's it!
I am struggling to make my basic lockscreen app.starting, the app displays a basic activity screen, with an image view (displaying an image from storage) and a TextView (displaying date & time).On swiping up, the activity goes back. What I am struggling with is, when screen is turned off & turned on, the event receiver should send out this event & my activity should come to foreground again. That's not what's happening. I am not completely sure how to use my service & broadcastreceiver to achieve this. Any help would be appreciated please.
My AndroidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kjtest2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<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/Theme.AppCompat.Light.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<receiver
android:name="com.example.kjtest2.EventsReciever"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF"/>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service
android:enabled="true"
android:name="com.example.kjtest2.myService"/>
<activity
android:name="com.example.kjtest2.MainActivity"
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" />
</intent-filter>
</activity>
</application>
My receiver:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class EventsReciever extends BroadcastReceiver {
public boolean wasScreenOn = true;
#Override
public void onReceive(Context context, Intent recievedIntent) {
Log.i("Check","[BroadCastReciever] onRecieve()");
if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wasScreenOn = false;
Log.i("Check","[BroadCastReciever] Screen went OFF");
} else if (recievedIntent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wasScreenOn = true;
Log.i("Check","[BroadCastReciever] Screen went ON");
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
}
else if(recievedIntent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
}
}
}
My onCreate code in the activity:
protected void onCreate(Bundle savedInstanceState) {
Log.i("event", "activity onCreate");
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, EventsReciever.class);
setContentView(R.layout.activity_main);
/** REMOVING KEYGUARD RECEIVER **/
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
setTime();
changeImage();
ImageView img2 = (ImageView)findViewById(R.id.imageView);
img2.setOnTouchListener(new OnSwipeTouchListener(this) {
#Override
public void onSwipeLeft() {
Toast.makeText(MainActivity.this, "Swipe left", Toast.LENGTH_SHORT)
.show();
changeImage();
Log.i("event","onSwipeLeft");
}
#Override
public void onSwipeRight() {
TextView tvDisplayDate = (TextView)findViewById(R.id.date1);
CustomDigitalClock cdc = (CustomDigitalClock)findViewById(R.id.dc1);
if (tvDisplayDate.getVisibility()==View.VISIBLE) {
tvDisplayDate.setVisibility(View.GONE);
cdc.setVisibility(View.GONE);
} else {
tvDisplayDate.setVisibility(View.VISIBLE);
cdc.setVisibility(View.VISIBLE);
}
}
#Override
public void onSwipeUp() {
Toast.makeText(MainActivity.this, "Swipe up", Toast.LENGTH_SHORT)
.show();
MainActivity.this.moveTaskToBack(true);
}
#Override
public void onSwipeDown() {
Toast.makeText(MainActivity.this, "Swipe down", Toast.LENGTH_SHORT)
.show();
}
});
}
And finally my service:
public class myService extends Service{
private NotificationManager mNM;
private int NOTIFICATION = R.string.local_service_started;
private final IBinder mBinder = new MyBinder();
private Messenger outMessenger;
#Override
public void onCreate() {
/** INITIALIZE RECEIVER **/
//RegisterReciever();
Log.i("event", "ServiceStarted");
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// Display a notification about us starting. We put an icon in the status bar.
showNotification();
}
}
As of now, the receiver & service aren't starting. I had been able to start them by registering the event dynamically, but I don't think that should be required. Registering the event in manifest should be sufficient for my application, isn't it?
Thanks for helping.
make sure your service is running by including startService(new Intent(this,MyService.class)); statement in your activity onCreate()
it is actually required to register receiver programmatically, manifest declaration will not work for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON, register the receiver instance in service onCreate(), unregister in onDestroy()
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
receiver = new EventsReciever();
registerReceiver(receiver, filter);
I have to schedule invoking of an already install test project on the device after 10 sec once the button is click. For that I have created an AlarmReceiver and my TaskService which actually is invoking the test project.
After running the app, nothing happens after the button is clicked. I don't know what is wrong but service doesn't performs the job even after 10 sec.
Below is my code which I am trying. The Activity class:
// Create an anonymous implementation of OnClickListener
private OnClickListener HTListener = new OnClickListener() {
public void onClick(View v) {
btnStartSchedule(v);
}
};
public void btnStartSchedule(View v) {
try {
AlarmManager alarms = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
intent.putExtra(AlarmReceiver.ACTION_ALARM, AlarmReceiver.ACTION_ALARM);
final PendingIntent pIntent = PendingIntent.getBroadcast(this, 1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pIntent);
Toast.makeText(ScrapeActivity.this, "Alarm Started", Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The AlarmReceiver class :
public class AlarmReceiver extends BroadcastReceiver {
public static String ACTION_ALARM = "com.alarmmanager.alarm";
#Override
public void onReceive(Context context, Intent intent) {
Log.i("Alarm Receiver", "Entered");
Toast.makeText(context, "Entered", Toast.LENGTH_SHORT).show();
Bundle bundle = intent.getExtras();
String action = bundle.getString(ACTION_ALARM);
if (action.equals(ACTION_ALARM)) {
Log.i("Alarm Receiver", "If loop");
Intent inService = new Intent(context, LaunchService.class);
context.startService(inService);
} else {
Log.i("Alarm Receiver", "Else loop");
Toast.makeText(context, "Else loop", Toast.LENGTH_SHORT).show();
}
}
}
And finally the LaunchService class:
public class LaunchService extends IntentService {
public LaunchService(String name) {
super(name);
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.my.testproject");
startActivity(LaunchIntent);
}
}
I have also mention the receiver and service inside the manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.pb.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/PB_APP"
android:theme="#style/AppTheme" >
<activity
android:name="com.my.pb.activity.ScrapeActivity"
android:label="#string/PB_APP" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AlarmReceiver"
android:process=":remote" >
</receiver>
<service android:name=".LaunchService" >
</service>
</application>
I don't understand whats the issue coz there's no exception in the Logcat. As I click the button, i just see the toast message saying Alarm Started. Please help.
You have a typo at the Manifest, you declared AlaramReceiver instead of .AlarmReceiver there.
My English is poor. I cannot start an android service in a boot time and I do not know the problem. I was trying example codes, but without success. Can somebody send me a project in Java that runs? Other code works for other people but on my tablet smartphone emulator it does not work. Does a problem exist in android 4.0?
This is my code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.service22"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="internalOnly"
>
<supports-screens android:largeScreens="false" android:normalScreens="true" android:smallScreens="false"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<service android:name="com.example.MyService">
<intent-filter>
<action android:name="com.example.MyService">
</action>
</intent-filter>
</service>
<receiver android:name="com.example.MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
</manifest>
public class MyService extends Service
{
private static final String LOG_TAG = "::Monitor";
#Override
public void onCreate() {
super.onCreate();
Log.e(LOG_TAG, "Service created.");
}
#Override
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
for (int i = 0 ; i < 20 ; i++)
{
mensaje();
}
Log.e(LOG_TAG, "Service started.");
}
#Override
public void onDestroy()
{
super.onDestroy();
Log.e(LOG_TAG, "Service destroyed.");
}
#Override
public IBinder onBind(Intent intent)
{
Log.e(LOG_TAG, "Service bind.");
return null;
}
public void mensaje()
{
Toast.makeText(this, "Hola", Toast.LENGTH_LONG).show();
}
}
public class MyReceiver extends BroadcastReceiver
{
public MyReceiver()
{
}
String LOG_TAG = "::StartAtBootServiceReceiver";
#Override
public void onReceive(Context context, Intent intent)
{
Log.e(LOG_TAG, "onReceive:");
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("com.example.MyService");
context.startService(i);
}
}
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class OnBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("OnBootReceiver", "Hi, Mom!");
}
}
and manifest file
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/cw"
android:label="#string/app_name">
<receiver android:name=".OnBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
I think:
Intent i = new Intent();
i.setAction("com.example.MyService");
context.startService(i);
is not enough,you must set intent class name like that:
Intent i = new Intent();
i.setClassName("com.example", "com.example.MyService");
i.setAction("com.example.MyService");
context.startService(i);
pls try that.
IMPORTANT: Android OS 3.1+ remains ignorent about your broadcast receivers in following cases:
1.User has never started the application explicitly at least once.
2.User has "force closed" the application.
Issue has nothing to do to your implementation. This is a potential security hole in Android that Google has closed :)