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

时间:2012-04-06 21:21:56

标签: 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();
    }
}

Reboot.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();
    }

}

widget.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>

widgetstuff.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没有给我任何错误,我也没有让任何力量关闭。小部件上的按钮根本不起作用 - 为什么?

2 个答案:

答案 0 :(得分:2)

改变这个:

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

到此:

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

原因:您无法将两个命令分开,因为 su 会单独运行而重启会在没有 su 的情况下运行

P.S。别忘了添加所需的权限

答案 1 :(得分:1)

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

<uses-permission android:name="android.permission.REBOOT" />