Java Code Examples for android.app.Application#getSystemService()

The following examples show how to use android.app.Application#getSystemService() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UnlockDeviceAndroidJUnitRunner.java    From Stock-Hawk with Apache License 2.0 6 votes vote down vote up
@SuppressLint("MissingPermission")
@Override
public void onStart() {
    Application application = (Application) getTargetContext().getApplicationContext();
    String simpleName = UnlockDeviceAndroidJUnitRunner.class.getSimpleName();
    // Unlock the device so that the tests can input keystrokes.
    ((KeyguardManager) application.getSystemService(KEYGUARD_SERVICE))
            .newKeyguardLock(simpleName)
            .disableKeyguard();
    // Wake up the screen.
    PowerManager powerManager = ((PowerManager) application.getSystemService(POWER_SERVICE));
    mWakeLock = powerManager.newWakeLock(FULL_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP |
            ON_AFTER_RELEASE, simpleName);
    mWakeLock.acquire();
    super.onStart();
}
 
Example 2
Source File: UnlockDeviceAndroidJUnitRunner.java    From incubator-taverna-mobile with Apache License 2.0 6 votes vote down vote up
@SuppressLint("MissingPermission")
@Override
public void onStart() {
    Application application = (Application) getTargetContext().getApplicationContext();
    String simpleName = UnlockDeviceAndroidJUnitRunner.class.getSimpleName();
    // Unlock the device so that the tests can input keystrokes.
    ((KeyguardManager) application.getSystemService(Context.KEYGUARD_SERVICE))
            .newKeyguardLock(simpleName)
            .disableKeyguard();
    // Wake up the screen.
    PowerManager powerManager = ((PowerManager) application.getSystemService(Context
            .POWER_SERVICE));
    mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, simpleName);
    mWakeLock.acquire();
    super.onStart();
}
 
Example 3
Source File: UnlockDeviceAndroidJUnitRunner.java    From ribot-app-android with Apache License 2.0 6 votes vote down vote up
@SuppressLint("MissingPermission")
@Override
public void onStart() {
    Application application = (Application) getTargetContext().getApplicationContext();
    String simpleName = UnlockDeviceAndroidJUnitRunner.class.getSimpleName();
    // Unlock the device so that the tests can input keystrokes.
    ((KeyguardManager) application.getSystemService(KEYGUARD_SERVICE))
            .newKeyguardLock(simpleName)
            .disableKeyguard();
    // Wake up the screen.
    PowerManager powerManager = ((PowerManager) application.getSystemService(POWER_SERVICE));
    mWakeLock = powerManager.newWakeLock(FULL_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP |
            ON_AFTER_RELEASE, simpleName);
    mWakeLock.acquire();
    super.onStart();
}
 
Example 4
Source File: Utils.java    From HJMirror with MIT License 6 votes vote down vote up
/**
 * wifi获取ip
 */
public String getIp(Application context) {
    try {
        //获取wifi服务
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifiManager == null) {
            return null;
        }
        //判断wifi是否开启
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int ipAddress = wifiInfo.getIpAddress();
        String ip = intToIp(ipAddress);
        return ip;
    } catch (Exception e) {

    }
    return null;
}
 
Example 5
Source File: RecordsStore.java    From StallBuster with Apache License 2.0 6 votes vote down vote up
/**
 * save one activity record
 * @param activityRecord record to save
 * @return the file path this record is saved
 */
public static boolean saveOneRecord(ActivityRecord activityRecord) {
    boolean ret = saveOneRecord(activityRecord.toJsonString());
    if (ret) {
        Application app = StallBuster.getInstance().getApp();
        Intent intent = new Intent(StallBuster.getInstance().getApp(), ReportListActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(StallBuster.getInstance().getApp(), 1, intent, FLAG_UPDATE_CURRENT);
        NotificationManager nm = (NotificationManager)app.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(NOTIFICATION_ID);
        Notification notification = new NotificationCompat.Builder(app)
                .setAutoCancel(true)
                .setContentTitle(app.getString(R.string.notification_title, activityRecord.cost, activityRecord.activity_name))
                .setContentText(app.getString(R.string.notification_text))
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .setContentIntent(pendingIntent)
                .build();
        nm.notify(NOTIFICATION_ID, notification);
    }
    return ret;
}
 
Example 6
Source File: MacAddressUtils.java    From CacheEmulatorChecker with Apache License 2.0 6 votes vote down vote up
public static String getConnectedWifiMacAddress(Application context) {
    String connectedWifiMacAddress = null;
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    List<ScanResult> wifiList;

    if (wifiManager != null) {
        wifiList = wifiManager.getScanResults();
        WifiInfo info = wifiManager.getConnectionInfo();
        if (wifiList != null && info != null) {
            for (int i = 0; i < wifiList.size(); i++) {
                ScanResult result = wifiList.get(i);
                if (!TextUtils.isEmpty(info.getBSSID()) && info.getBSSID().equals(result.BSSID)) {
                    connectedWifiMacAddress = result.BSSID;
                }
            }
        }
    }
    return connectedWifiMacAddress;
}
 
Example 7
Source File: CCUtil.java    From CC with Apache License 2.0 6 votes vote down vote up
/**
 * 获取当前进程的名称
 * @return 进程名
 */
public static String getCurProcessName() {
    if (curProcessName != null) {
        return curProcessName;
    }
    Application application = CC.getApplication();
    if (application == null) {
        return PROCESS_UNKNOWN;
    }
    try {
        ActivityManager manager = (ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
            for (RunningAppProcessInfo appProcess : processes) {
                if (appProcess.pid == android.os.Process.myPid()) {
                    curProcessName = appProcess.processName;
                    return curProcessName;
                }
            }
        }
    } catch (Exception e){
        CCUtil.printStackTrace(e);
    }
    return PROCESS_UNKNOWN;
}
 
Example 8
Source File: CCUtil.java    From CC with Apache License 2.0 6 votes vote down vote up
public static String[] getCurProcessPkgList() {
    Application application = CC.getApplication();
    if (application == null) {
        return null;
    }
    try {
        ActivityManager manager = (ActivityManager) application.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            List<RunningAppProcessInfo> processes = manager.getRunningAppProcesses();
            for (RunningAppProcessInfo appProcess : processes) {
                if (appProcess.pid == android.os.Process.myPid()) {
                    return appProcess.pkgList;
                }
            }
        }
    } catch (Exception e){
        CCUtil.printStackTrace(e);
    }
    return null;
}
 
Example 9
Source File: AppGcmReceiverUIBackground.java    From RxGcm with Apache License 2.0 6 votes vote down vote up
private void buildAndShowNotification(Message message) {
    backgroundMessage = message;

    Bundle payload = message.payload();
    Application application = message.application();

    String title = payload.getString(GcmServerService.TITLE);
    String body = payload.getString(GcmServerService.BODY);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(application)
            .setContentTitle(title)
            .setContentText(body)
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setAutoCancel(true)
            .setContentIntent(getPendingIntentForNotification(application, message));

    NotificationManager notificationManager = (NotificationManager) application.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notificationBuilder.build());
}
 
Example 10
Source File: EasyBLE.java    From easyble-x with Apache License 2.0 5 votes vote down vote up
public synchronized void initialize(@NonNull Application application) {
    if (isInitialized()) {
        return;
    }
    Inspector.requireNonNull(application, "application can't be");
    this.application = application;
    //检查是否支持BLE
    if (!application.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        return;
    }
    //获取蓝牙配置器
    BluetoothManager bluetoothManager = (BluetoothManager) application.getSystemService(Context.BLUETOOTH_SERVICE);
    if (bluetoothManager == null || bluetoothManager.getAdapter() == null) {
        return;
    }
    bluetoothAdapter = bluetoothManager.getAdapter();
    //注册蓝牙开关状态广播接收者
    if (broadcastReceiver == null) {
        broadcastReceiver = new InnerBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        application.registerReceiver(broadcastReceiver, filter);
    }        
    isInitialized = true;
}
 
Example 11
Source File: MyApplication.java    From My-MVP with Apache License 2.0 5 votes vote down vote up
/**
 * is main process
 */
private static boolean isMainProcess(Application application) {
    int pid = android.os.Process.myPid();
    String processName = "";
    ActivityManager manager = (ActivityManager) application.getSystemService
            (Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
        if (process.pid == pid) {
            processName = process.processName;
        }
    }
    return application.getPackageName().equals(processName);
}
 
Example 12
Source File: TestRunner.java    From Spork with Apache License 2.0 5 votes vote down vote up
@Override
public void callApplicationOnCreate(Application app) {
    // Unlock the screen
    KeyguardManager keyguard = (KeyguardManager) app.getSystemService(Context.KEYGUARD_SERVICE);
    keyguard.newKeyguardLock(getClass().getSimpleName()).disableKeyguard();

    // Start a wake lock
    PowerManager power = (PowerManager) app.getSystemService(Context.POWER_SERVICE);
    mWakeLock = power.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, getClass().getSimpleName());
    mWakeLock.acquire();

    super.callApplicationOnCreate(app);
}
 
Example 13
Source File: MyApplication.java    From My-MVP with Apache License 2.0 5 votes vote down vote up
/**
 * is main process
 */
private static boolean isMainProcess(Application application) {
    int pid = android.os.Process.myPid();
    String processName = "";
    ActivityManager manager = (ActivityManager) application.getSystemService
            (Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
        if (process.pid == pid) {
            processName = process.processName;
        }
    }
    return application.getPackageName().equals(processName);
}
 
Example 14
Source File: TestRunner.java    From Spork with Apache License 2.0 5 votes vote down vote up
@Override
public void callApplicationOnCreate(Application app) {
    // Unlock the screen
    KeyguardManager keyguard = (KeyguardManager) app.getSystemService(Context.KEYGUARD_SERVICE);
    keyguard.newKeyguardLock(getClass().getSimpleName()).disableKeyguard();

    // Start a wake lock
    PowerManager power = (PowerManager) app.getSystemService(Context.POWER_SERVICE);
    mWakeLock = power.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, getClass().getSimpleName());
    mWakeLock.acquire();

    super.callApplicationOnCreate(app);
}
 
Example 15
Source File: IMMLeaks.java    From timecat with Apache License 2.0 5 votes vote down vote up
/**
 * Fix for https://code.google.com/p/android/issues/detail?id=171190 .
 * <p>
 * When a view that has focus gets detached, we wait for the main thread to be idle and then
 * check if the InputMethodManager is leaking a view. If yes, we tell it that the decor view got
 * focus, which is what happens if you press home and come back from recent apps. This replaces
 * the reference to the detached view with a reference to the decor view.
 * <p>
 * Should be called from {@link Activity#onCreate(android.os.Bundle)} )}.
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void fixFocusedViewLeak(Application application) {

    // Don't know about other versions yet.
    if (SDK_INT < KITKAT || SDK_INT > 23) {
        return;
    }

    final InputMethodManager inputMethodManager = (InputMethodManager) application.getSystemService(INPUT_METHOD_SERVICE);

    final Field mServedViewField;
    final Field mHField;
    final Method finishInputLockedMethod;
    final Method focusInMethod;
    try {
        mServedViewField = InputMethodManager.class.getDeclaredField("mServedView");
        mServedViewField.setAccessible(true);
        mHField = InputMethodManager.class.getDeclaredField("mServedView");
        mHField.setAccessible(true);
        finishInputLockedMethod = InputMethodManager.class.getDeclaredMethod("finishInputLocked");
        finishInputLockedMethod.setAccessible(true);
        focusInMethod = InputMethodManager.class.getDeclaredMethod("focusIn", View.class);
        focusInMethod.setAccessible(true);
    } catch (NoSuchMethodException | NoSuchFieldException unexpected) {
        Log.e("IMMLeaks", "Unexpected reflection exception", unexpected);
        return;
    }

    application.registerActivityLifecycleCallbacks(new LifecycleCallbacksAdapter() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            ReferenceCleaner cleaner = new ReferenceCleaner(inputMethodManager, mHField, mServedViewField, finishInputLockedMethod);
            View rootView = activity.getWindow().getDecorView().getRootView();
            ViewTreeObserver viewTreeObserver = rootView.getViewTreeObserver();
            viewTreeObserver.addOnGlobalFocusChangeListener(cleaner);
        }
    });
}
 
Example 16
Source File: NetworkStatus.java    From FriendlyDemo with Apache License 2.0 4 votes vote down vote up
@Inject
public NetworkStatus(Application context) {
    connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
    wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
}
 
Example 17
Source File: BarricadeShakeListener.java    From Barricade with Apache License 2.0 4 votes vote down vote up
public BarricadeShakeListener(Application application) {
  this.application = application;
  this.sensorManager = (SensorManager) application.getSystemService(SENSOR_SERVICE);

  application.registerActivityLifecycleCallbacks(this);
}
 
Example 18
Source File: NavigationViewModel.java    From graphhopper-navigation-android with MIT License 4 votes vote down vote up
private void initializeConnectivityManager(Application application) {
  connectivityManager = (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
}
 
Example 19
Source File: NetModule.java    From ello-android with MIT License 4 votes vote down vote up
@Provides
@Singleton
ConnectivityManager providesConnectivityManager(Application application) {
    return (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
}
 
Example 20
Source File: RealmBufferResolver.java    From TimberLorry with Apache License 2.0 4 votes vote down vote up
public RealmBufferResolver(Application application, Account account, String name) {
    this(application, (AccountManager) application.getSystemService(Context.ACCOUNT_SERVICE), application.getContentResolver(), account, name);
}