1

我开始为此拔头发了!我一直在尝试制作一个应用程序,它将在屏幕上放置一个小部件,单击该小部件将重新启动手机。小部件出现在屏幕上,但单击时没有任何反应。我一直在 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>

小部件stuff.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>

小部件布局.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 没有给我任何错误,而且我没有得到任何强制关闭。小部件上的按钮根本不做任何事情 - 为什么?

4

2 回答 2

2

改变这个:

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

对此:

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

原因:你不能分开这两个命令,因为然后su单独运行并且reboot在没有su的情况下运行

PS不要忘记添加所需的权限

于 2012-04-06T21:26:01.777 回答
1

清单中缺少权限。请注意, il 仅适用于实际设备,不适用于模拟器

<uses-permission android:name="android.permission.REBOOT" />
于 2012-04-06T21:28:11.560 回答