gpt4 book ai didi

java - 锁屏小部件不工作并且不显示任何内容

转载 作者:行者123 更新时间:2023-11-30 01:46:47 25 4
gpt4 key购买 nike

I'm developing screen lock widgets or Lock Screen programmatically. I got some good ideas from Stackoverflow previous questions regarding this, and I did some thing well, but when I run that code, there is No Exception and Error. but when I run it shows nothing. what I want is when user click on widgets it locks the screen.

Manifest.xml file

`

<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

公共(public)类 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

公共(public)类 AdminManageReceiver 扩展 DeviceAdminReceiver {

最佳答案

首先,您可能无法使用单个 <receiver>对于 DeviceAdminReceiver 一个AppWidgetProvider .与应用程序小部件一起使用的 Android 部件可能不包含 android.permission.BIND_DEVICE_ADMIN许可,阻止他们使用您的 <receiver> .使用两个单独的 <receiver>元素,一个给你的DeviceAdminReceiver还有一个给你的 AppWidgetProvider .

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

关于java - 锁屏小部件不工作并且不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33611332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com