java - 将重启手机的 Android 应用程序小部件 - 小部件没有做任何事情?

标签 java android android-widget

我开始为此烦恼了!我一直在尝试制作一个应用程序,它将在屏幕上放置一个小部件,单击该小部件将重启手机。该小部件出现在屏幕上,但单击时没有任何反应。我一直在关注 Youtube 上 Bucky 的教程。

代码在这里,我不知道我做错了什么!

reboot_widget_activeity.java:

package com.liamwli.reboot_widget;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class reboot_widget_activeity extends Activity implements
        OnClickListener {
    /** Called when the activity is first created. */
    int awID;
    AppWidgetManager awm;
    Context c = reboot_widget_activeity.this;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        Intent i = getIntent();
        Bundle extras = i.getExtras();
        if (extras != null) {
            awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        awm = AppWidgetManager.getInstance(c);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);

        Intent in = new Intent(c, Reboot.class);
        PendingIntent pi = PendingIntent.getActivity(c, 0, in, 0);
        views.setOnClickPendingIntent(R.id.bRB, pi);

        awm.updateAppWidget(awID, views);

        Intent result = new Intent();
        result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
        setResult(RESULT_OK, result);
        finish();
    }
}

重启.java:

package com.liamwli.reboot_widget;

import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.Toast;

public class Reboot extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        ProgressDialog wait;
        wait = new ProgressDialog(Reboot.this);
        wait.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        wait.setMessage("Rebooting. If this hangs, then this app wont work on this device (pull battery)");
        wait.setCancelable(false);
        wait.show();
        try {
            Runtime.getRuntime().exec("su");
            Runtime.getRuntime().exec("reboot");
        } catch (IOException e) {
        }
        wait.dismiss();
        Toast.makeText(Reboot.this, "Unable to reboot. Please ensure your device is rooted!", Toast.LENGTH_LONG).show();
        finish();
    }

}

小部件.java:

package com.liamwli.reboot_widget;

import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.Toast;

public class Widget extends AppWidgetProvider {

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // TODO Auto-generated method stub
        super.onDeleted(context, appWidgetIds);
        Toast.makeText(context, "Reboot Widget Added", Toast.LENGTH_SHORT)
                .show();
    }


}

androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.liamwli.reboot_widget"
    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=".reboot_widget_activeity"
            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=".Widget">

        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

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

    <activity
            android:name=".Reboot"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.liamwli.reboot_widget.REBOOT" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>




    </application>

</manifest>

小部件.xml:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minHeight="40dp" android:minWidth="40dp" android:initialLayout="@layout/widget_layout">


</appwidget-provider>

widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bRB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reboot" />

</LinearLayout>

Eclipse 没有给我任何错误,我也没有强制关闭。小部件上的按钮根本不执行任何操作 - 为什么?

最佳答案

改变这个:

Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("reboot");

为此:

Runtime.getRuntime().exec("su -c reboot");

原因:您不能将这两个命令分开,因为 su 会单独运行,reboot 会在没有 su 的情况下运行p>

附言不要忘记添加所需的权限

关于java - 将重启手机的 Android 应用程序小部件 - 小部件没有做任何事情?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10049413/

相关文章:

java - 您将如何从一个类的所有实例中调用一个方法?

android - 高通 SNPE 阻止 UI 线程

android - 如何使用 Canvas 在位图图像上设置 onTouchEvent。

android - 如何从 Android 中的 AppWidget 获取正确的 DisplayMetrics?

java - Android 主屏幕小部件

java - 大型同义词集数据集中的 WordNetSimalarity

java - 更改 Log4j 中的优先级

java - 两个大小相同的 jar 显示不同的校验和,为什么会这样?

java - Android 更新每个操作的值总和

java - 带有 view.getLocationOnScreen 的 NullPointerException