锁定屏幕小部件不工作,它不显示任何内容

时间:2015-11-09 14:27:59

标签: java android xml

  

我正在以编程方式开发屏幕锁定小部件或锁定屏幕。我从Stackoverflow得到了一些关于此问题的好主意,我做了一些事情,但是当我运行该代码时,没有异常和错误。但是当我跑的时候什么都没显示。   我想要的是当用户点击小工具时它会锁定屏幕。

     

Manifest.xml文件

`

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    <activity
        android:name=".MainActivity"
        android:theme="@android:style/Theme.NoDisplay"
        android:label="@string/app_name"
        android:excludeFromRecents="true" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
            <action android:name="android.intent.action.ASSIST" />
        </intent-filter>
    </activity>

    <receiver android:name=".NewAppWidget"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/new_app_widget_info" />
    </receiver>
</application>

`

  

MainActivity.java

公共类MainActivity扩展了Activity {

final static int ENABLE_ADMIN = 1;
final static int SUCESS = -1;

private ComponentName mAdminName = null;

public final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mAdminName = new ComponentName(this, AdminManageReceiver.class);

    DevicePolicyManager mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);

    if (!mDevicePolicyManager.isAdminActive(mAdminName)) {
        showAdminManagement();
    }

    if (mDevicePolicyManager.isAdminActive(mAdminName)) {
        mDevicePolicyManager.lockNow();
    }
    else {
        Log.e("screenlock", "Unable to lock the phone D:");
    }

    finish();
}

private void showAdminManagement() {
    // TODO Auto-generated method stub
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
            R.string.desc_enable_admin);
    startActivityForResult(intent, ENABLE_ADMIN);

}

}

  

Appwidgetprovider.java

public class NewAppWidget extends AppWidgetProvider {
private static final int ADMIN_INTENT = 15;
private static final String description = "Sample Administrator description";
private DevicePolicyManager mDevicePolicyManager;
private ComponentName mComponentName;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        //updateAppWidget(context, appWidgetManager, appWidgetIds[lock]);
        Intent intent = new Intent(context,MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        RemoteViews View = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
        View.setOnClickPendingIntent(R.id.imageButton, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, View);


    }
}
  

AdminManageReceiver.java

公共类AdminManageReceiver扩展了DeviceAdminReceiver { }

1 个答案:

答案 0 :(得分:1)

首先,您可能无法为<receiver> DeviceAdminReceiver使用单个AppWidgetProvider。与app小部件一起使用的Android部分可能不会获得android.permission.BIND_DEVICE_ADMIN权限,阻止他们使用您的<receiver>。使用两个单独的<receiver>元素,一个用于DeviceAdminReceiver,另一个用于AppWidgetProvider

其次,请记住锁定屏幕上的应用小部件仅在Android 4.2到4.4上受支持。较早版本和较新版本的Android不支持此功能。